I am fresher in android.In my project I have one layout common for all activities. And I am trying to reuse my layout in Android.Following is my code
MainActivity
public class MainActivity extends Activity implements OnClickListener {
EditText emailEdit, passwordEdit;
Button loginButton;
TitleBarLayout titlebarLayout;
String r;
String rr;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
emailEdit = (EditText) findViewById(R.id.etEmail);
passwordEdit = (EditText) findViewById(R.id.etPassword);
loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(this);
//titlebarLayout=(TitleBarLayout) findViewById(R.id.titlebar);
titlebarLayout=new TitleBarLayout(MainActivity.this);
titlebarLayout.setLeftButtonText("Refresh");
titlebarLayout.setRightButtonText("Logout");
titlebarLayout.setTitle("iProtect");
OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.left_button)
{}
else if(v.getId()==R.id.right_button)
{}
}
};
titlebarLayout.setLeftButtonOnClickListener(listener);
titlebarLayout.setRightButtonOnClickListener(listener);
}
}
login_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/layout_bg_color">
<include layout="@layout/titlebar_layout"
android:id="@+id/titlebar"/>
<EditText
android:id="@+id/etEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@layout/titlebar_layout"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dp"
android:layout_marginTop="100dip"
android:background="@drawable/emailedit"
android:inputType="textEmailAddress"
android:maxEms="20"
android:paddingLeft="70dip"
android:singleLine="true"
android:text="user@gmail.com" />
<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etEmail"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dp"
android:layout_marginTop="0dip"
android:background="@drawable/passwordedit"
android:inputType="textPassword"
android:maxEms="20"
android:paddingLeft="70dip"
android:singleLine="true"
android:text="123456" />
<Button
android:id="@+id/loginButton"
android:layout_width="@dimen/login_button_width"
android:layout_height="@dimen/login_button_height"
android:layout_below="@+id/etPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
android:text="Login"
android:textStyle="bold"
android:background="@android:color/white"/>
<ImageView
android:id="@+id/ivFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_below="@+id/loginButton"
android:layout_marginTop="360dip"
android:adjustViewBounds="true"
android:baselineAlignBottom="false"
android:cropToPadding="false"
android:src="@drawable/footer" />
</RelativeLayout>
titleBarlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/titlebar_height"
android:background="@color/title_bg_color"
android:gravity="center_vertical"
android:orientation="horizontal">
<Button
android:id="@+id/left_button"
android:layout_width="@dimen/titlebar_button_width"
android:layout_height="@dimen/titlebar_button_height"
android:background="#000000"/>
<TextView
android:id="@+id/title_textview"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textStyle="bold" />
<Button
android:id="@+id/right_button"
android:layout_width="@dimen/titlebar_button_width"
android:layout_height="@dimen/titlebar_button_height" />
</LinearLayout>
And TitleBarLayout.java
public class TitleBarLayout extends LinearLayout {
private WeakReference<Activity> activityRef;
//Button leftButton, rightButton;
private View contentView;
Button leftButton,rightButton;
TextView titletext;
public TitleBarLayout(Activity a) {
super(a);
Log.i("TitleBar Layout", "Inside constructor");
activityRef = new WeakReference<Activity>(a);
inflateViewsFromXml();
setListenersOnViews();
setValuesOnViews();
}
private void setValuesOnViews() {
// TODO Auto-generated method stub
}
private void setListenersOnViews() {
// TODO Auto-generated method stub
}
private void inflateViewsFromXml() {
Activity a = activityRef.get();
LayoutInflater inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.titlebar_layout, null);
}
public void setLeftButtonText(int resID) {
}
public void setLeftButtonText(String resID) {
Log.v("Button", "Button Text="+resID);
if (contentView != null) {
leftButton = (Button)contentView.findViewById(R.id.left_button);
Log.i("ERROR", "Inside setLeftButtonText");
leftButton.setText(""+resID);
leftButton.setTextColor(Color.WHITE);
}
}
public void setLeftButtonOnClickListener(View.OnClickListener listener) {
leftButton.setOnClickListener(listener);
}
public void setRightButtonText(int resID) {
}
public void setRightButtonText(String resID) {
Log.v("Button", "Button Text="+resID);
if (contentView != null) {
Log.i("ERROR", "Inside setRightButtonText");
rightButton = (Button)contentView.findViewById(R.id.right_button);
rightButton.setTextColor(Color.BLACK);
rightButton.setText(""+resID);
}
}
public void setRightButtonOnClickListener(View.OnClickListener listener) {
rightButton.setOnClickListener(listener);
}
public void setTitle(int resID) {
}
public void setTitle(String resID) {
//@string/app_name
Log.v("TextView", "Text="+resID);
if (contentView == null) {
Log.i("ERROR", "Inside setTitle");
titletext = (TextView)contentView.findViewById(R.id.title_textview);
titletext.setText(""+resID);
}
}
}
I want to add text on buttons at runtime.But I cant get it? I can see text on Logcat but not on button?
You are inflating the layout into the variable view ‘contentView’.
But because your class is the view that you are inflating in the xml file. You should inflate is as following:
Then the layout will be visible on the screen.
This way the inflate will add the inflated view to the
LinearLayoutwhere your class is extending from.