Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8539151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:17:43+00:00 2026-06-11T11:17:43+00:00

I am fresher in android.In my project I have one layout common for all

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:17:44+00:00Added an answer on June 11, 2026 at 11:17 am

    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:

        inflater.inflate(R.layout.titlebar_layout, this);
    

    Then the layout will be visible on the screen.

    This way the inflate will add the inflated view to the LinearLayout where your class is extending from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a fresher and I have got a project in asp.net and c#.
Consider one .txt file.. in that i have no of paragraphs separated by a
I'm a fresher with WPF, MVVM. Now, I have been investigating MVVM Light. I
hi i am fresher now i am working android application...tell me how to create
I am a fresher in c#. I have developed an application that can convert
I have website http://yournextleap.com/fresher-jobs Please find out last div having HTML <div data-recommendation= data-id=3790
Before today i use Eclipse 3.8 with Sequoyah plugin for Android NDK project. But
I am a fresher to web development. In my application I need to have
I have a question: In Java, I have a class Fresher, which extends Employee
I have the following strings: I submit the following values: username 'foo', password 'bar'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.