Minggu, 15 Desember 2013

Set layout programmatically

This example show how to set paddings and margins programmatically with Java code.

set paddings and margins programmatically
set paddings and margins programmatically

package com.example.androidadjlayout;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

LinearLayout mainLayout;
SeekBar settingPadding, settingMargin2;
ImageView image1, image2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout)findViewById(R.id.mainlayout);
settingPadding = (SeekBar)findViewById(R.id.setmainpadding);
settingMargin2 = (SeekBar)findViewById(R.id.setimage2margin);
image1 = (ImageView)findViewById(R.id.image1);
image2 = (ImageView)findViewById(R.id.image2);

settingPadding.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mainLayout.setPadding(progress, progress, progress, progress);
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
}

@Override
public void onStopTrackingTouch(SeekBar arg0) {
}});

settingMargin2.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
LinearLayout.LayoutParams layoutParams = (LayoutParams) image2.getLayoutParams();
layoutParams.setMargins(progress, progress, progress, progress);
image2.setLayoutParams(layoutParams);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}});
}

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#303030" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#A00000"
android:textStyle="bold|italic"
android:text="android-coding.blogspot.com" />
<SeekBar
android:id="@+id/setmainpadding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="5"/>
<SeekBar
android:id="@+id/setimage2margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="5"/>
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:background="#505050"/>
<ImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:src="@drawable/ic_launcher"
android:background="#808080" />

</LinearLayout>

0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved