in my Android app there are two Activity (Main and Settings) with main.xml and settings.xml. I want to create a RadioGroup in Settings that works like a settings panel for users so they can select Backgroundcolor of Buttons. I'm trying to use onClick with each RadioButton and it work well but if I come back to main.xml che color is not saved. How can I create a settings panel for set BackgroundColor of Buttons in other activity and save it (or with a Save Button for users)?
Here, Settings' code:
public class Settings extends Main implements OnCheckedChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
final Button back;
back= (Button) findViewById(R.id.back);
RadioButton redbtn, blubtn, grebtn;
redbtn= (RadioButton) findViewById(R.id.redbtn);
blubtn= (RadioButton) findViewById(R.id.blubtn);
grebtn= (RadioButton) findViewById(R.id.grebtn);
redbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
back.setBackgroundColor(Color.RED);
add.setBackgroundColor(Color.RED);
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
}
}
settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/title_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/settings"
android:textSize="40sp" />
<TextView
android:id="#+id/ButtonSettingsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/title_settings"
android:layout_marginTop="17dp"
android:text="#string/BtnSet"
android:textSize="30sp" />
<Button
android:id="#+id/back"
android:layout_width="70sp"
android:layout_height="70sp"
android:layout_alignBaseline="#+id/title_settings"
android:layout_alignBottom="#+id/title_settings"
android:layout_alignRight="#+id/ButtonSettingsView"
android:text="Back"/>
<RadioGroup
android:id="#+id/ButtonSettings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ButtonSettingsView"
android:layout_marginTop="14dp"
android:orientation="vertical" >
<RadioButton
android:id="#+id/redbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/red"/>
<RadioButton
android:id="#+id/blubtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/blue" />
<RadioButton
android:id="#+id/grebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/grey"/>
</RadioGroup>
</RelativeLayout>
Thanks
Of course it won't save it for other Activities because when you start another then that Activity will use whatever its default is in xml. You need to save the color somehow then reference it in your other Activities. One way to do this, if you want it to persist when the user leaves and reenters the app, is to use SharedPreferences. Then you can open up these prefs in your other Activites, value saved, and change background accordingly.
If you only want it to change the color for the time that the user is currently in the app then you could create a static class to store this variable and change the colors according to that value.
Related
i've a problem. I want to make a app with a loggin activity and a main activity. (To the OnClickListiner later)
fist:
What ive done so far:
i've created a login. java and login.xml
login.java:
public class Login extends Activity implements OnClickListener {
Button btnStartAnotherActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
boolean hasLogedIn = true;
if (hasLogedIn) {
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
finish();
} else {
}
}
public void onClick(View view) {
//calling an activity using <intent-filter> action name
Intent inent = new Intent("android.name.MainActivity ");
startActivity(inent);
}
}
i've created a MainActivity.java and activity_main.xml
moreover i've some other java and xml files to make a materiel designed Tab view for my MainActivity.
i have 3 tabs thats how it looks so far
[![tab1 with buttons][1]][1]
-now i'be added to the first tab 3 buttons.
example of one button: (the others are the same just a other id )
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kommt"
android:clickable="false"
android:textColor="#190707"
android:id="#+id/kommt"
android:layout_alignParentStart="true"
android:layout_below="#+id/space" />
-So the app is running now without problems. ( All Tabs and there content is showing like i want it )
when i build the project the mainactivity is open first( see that link of the picture )
HOW TO SET: the login interface to be started once, when starting the app for the first time. After login is succesfully, then the mainactivity will always open. Thats my first problem. What should i add to the Login.java ? and how to set that the login.xml starts before the mainactivity ?
Second:
As i told you ive added some buttons. To test the buttons i've tried to implement a code for toast notification when clicking on button. But every time i build the project with the toast notifitcation code, the app doenst start anymore. Here the code for the toast notification i'm using:
public class MainActivity extends ActionBarActivity implements OnClickListener {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // but my main_activity doesnt have buttons ... tab1.xml have b
Button kommtbutton;
kommtbutton= (Button) findViewById(R.id.kommt);
kommtbutton.setOnClickListener(this);}
#Override
public void onClick(View v) {
setContentView(R.layout.tab1); // not sure if this is right ive did it cause the buttons are in the tab1 layout and not main_activity
switch(v.getId())
{
case R.id.kommt:
{
Toast toast1 = Toast.makeText(getApplicationContext(),
"Eingestempelt",
Toast.LENGTH_SHORT);
toast1.show();
break;
} ....}
here i have implemented all 3 buttons in a switch case.
It looks right but the onclicklistinier seems to kill my application before it can start. Maybe someone can help me.
i have following files:
Login.java, MainActivity.java, Tab1.java,Tab2.java,Tab3.java, SlidingTabsLayout.java, SlidingTabStrip.java and ViewPagerAdaper.
and i have this layouts.
acitivy_main.xml, login.xml, tab1,tab2,tab3.xml, toolbar.xml.
I'm not allowed to send pictures cause i'm new here.
Where have i do implement the code for the toast notification ?
thats my activity_main:xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<android_package.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="#color/ColorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"/>
and this is my tab1.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Tabs"
android:background="#FDFDFE"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/buchungen"
android:textColor="#190707"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Geht"
android:clickable="false"
android:textColor="#190707"
android:id="#+id/geht"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/textClock"
android:layout_marginBottom="36dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name:"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:textColor="#190707"
android:id="#+id/name"
android:layout_alignParentStart="true"
android:layout_below="#+id/buchungen" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#190707"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Status:"
android:id="#+id/status"
android:layout_below="#+id/name"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#190707"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Letzte Buchung:"
android:paddingBottom="4dp"
android:paddingTop="7dp"
android:id="#+id/letzteBuchung"
android:layout_below="#+id/status"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="#+id/buchungen"
android:weightSum="1"
android:id="#+id/linearLayout">
<ImageView
android:layout_width="314dp"
android:layout_height="310dp"
android:id="#+id/profilbild"
android:layout_gravity="center_horizontal"
android:src="#drawable/time" />
</LinearLayout>
<Space
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_above="#+id/geht"
android:id="#+id/space" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#190707"
android:id="#+id/textClock"
android:layout_alignTop="#+id/name"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kommt"
android:clickable="false"
android:textColor="#190707"
android:id="#+id/kommt"
android:layout_alignParentStart="true"
android:layout_below="#+id/space" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Pause An/Aus"
android:textColor="#190707"
android:id="#+id/textView"
android:layout_alignTop="#+id/textView3"
android:layout_alignStart="#+id/pause" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Einstempeln"
android:textColor="#190707"
android:id="#+id/textView2"
android:layout_above="#+id/kommt"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Ausstempeln"
android:textColor="#190707"
android:id="#+id/textView3"
android:layout_alignBottom="#+id/geht"
android:layout_alignStart="#+id/geht"
android:layout_alignTop="#+id/textView2" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:textColor="#190707"
android:id="#+id/pause"
android:layout_alignBottom="#+id/kommt"
android:layout_centerHorizontal="true"
android:checked="false" />
</RelativeLayout>
The buttons are in the tab1.xml. Where have i to acces them to make a Interaction ( show toast when pressing the button ) ? in the MainActivity.java or the Tab1.java or somewhere else ?
When i try to add toast notification my app just kills itself ...
You have to put all the findViewById and setContentView in the onCreate method or they won't do the job.
First point :
public class MainActivity extends ActionBarActivity
{
// Layout elements
private Button kommtbutton = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Attach layout
setContentView(R.layout.activity_main); // but my main_activity doesnt have buttons ... tab1.xml have b
// Retrieve layout elements
kommtbutton= (Button) findViewById(R.id.kommt);
// Attach listeners
kommtbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
// Do not use getApplicationContext(), this is an activity
Toast.makeText(MainActivity.this, "Eingestempelt", Toast.LENGTH_SHORT).show();
}
});
}
[...]
}
Second point :
public class LoginActivity extends Activity
{
// Layout elements
private EditText edit_login = null;
private EditText edit_password = null;
private Button btn_login = null;
// Class variables
private SharedPreferences prefs = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Check if the user is already logged in
prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
if (prefs.getBoolean("isLoggedIn", false))
{
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
return;
}
// Attach layout
setContentView(R.layout.login);
// Retrieve layout elements
edit_login = (EditText) findViewById(R.id.edit_login);
edit_password = (EditText) findViewById(R.id.edit_password);
btn_login = (Button) findViewById(R.id.btn_login);
// Attach listeners
btn_login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
// Retrieve information
String login = edit_login.getText().toString();
String password = edit_password.getText().toString();
// Do job
boolean canConnect = true; // TODO
if (canConnect)
{
// Update prefs
prefs.edit().putBoolean("isLoggedIn", true).commit();
// Move to activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
else
{
// Update prefs
prefs.edit().putBoolean("isLoggedIn", false).commit();
// Display error message
Toast.makeText(LoginActivity.this, "Wrong crendentials", Toast.LENGTH_LONG).show();
}
}
});
}
}
Hy.... I'm making a simple project on eclipse. I'm using relative layout as shown below :
<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:gravity="center"
tools:context=".ProfileActivity"
>
<Button
android:id="#+id/Prof_edit_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
/>
<LinearLayout
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
>
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="invisible"
android:clickable="false"
/>
</LinearLayout>
</RelativeLayout>
In the main activity, I've made a simple onClicklistener for Edit and Save button as shown below :
mEdit = (Button)findViewById(R.id.Prof_edit_btn);
mEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mBatal.setVisibility(View.VISIBLE);
mBatal.setClickable(true);
mSave.setVisibility(View.VISIBLE);
mSave.setClickable(true);
mEdit.setVisibility(View.INVISIBLE);
}
});
mSave = (Button)findViewById(R.id.Prof_save_btn);
mSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mEdit.setVisibility(View.VISIBLE);
mBatal.setVisibility(View.INVISIBLE);
mSave.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Data Anda berhasil disimpan", Toast.LENGTH_SHORT).show();
}
});
Well... As we can see from my layout xml, only EDIT button will be visible on the start, then SAVE and BATAL button will be visible when EDIT is clicked by user according to the code program within setonclick listener of EDIT button. And as soon as EDIT button is clicked it will be invisible.... The problems here are WHen I clicked EDIT button, only SAVE button will be visible and BATAL button will be visible when SAVE button is clicked, another problem that I'm facing now is EDIT button is not invisible at all when I click it.... So.. anyone can help me with this problem please...??? :'(
Thanks in advance....
Naaah... I found my own solution... Just set the visibility of SAVE btn and BATAL btn to GONE... And it's all done.... :-)
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="gone"<-----------=
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="gone"<-----------=
android:clickable="false"
/>
In my android code, I have implemented onclick event on 3 textviews using following code. But nothing happens when they are clicked. What is wrong ?
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:textColor="#ffffff"
/>
public void selectit(View v)
{
Log.d("tv0","ok");
if(v.getId()==tv1.getId())
{Log.d("tv1","ok");
selectoption(1);
Log.d("tv1","ok");
}
if(v.getId()==tv2.getId())
{Log.d("tv2","ok");
selectoption(2);
}
if(v.getId()==tv3.getId())
{Log.d("tv3","ok");
selectoption(3);
}
}
Add this into your xml
android:clickable="true"
You probably need to have this code in your xml
android:clickable="true"
Or Set the Clicklistenner to TextView via code by
TextView btn=(TextView) findViewById(R.id.tv1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectit(v);
}
});
It will automatically make it clickable so no need of android:clickable="true"
You can set the click handler in xml with these attribute:
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:clickable="true"
android:textColor="#ffffff"
/>
i hope it will help u all the best
Try like below:
android:onClick="onClick"
android:clickable="true"
My textview:
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
Main Activity:
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
I have a custom dialog preference that have bunch of button.each button represent a number.something like clock application when you want to set alarm. how can I get onClick of each button in my dialog preference?
this is part of my dialog layout (time_picker) :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="#+id/showTimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="_ _ : _ _"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="#+id/Buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_below="#+id/showTimer"
android:layout_marginTop="10dp">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="1"
android:background="#android:color/transparent" />
<Button
android:id="#+id/button2"
android:layout_width="100dp"
...
this is Setting layout that launch dialog preference:
<com.example.test.TimerForNoti
android:key = "pref_sync_noti_timer"
android:title = "#string/pref_sync_noti_timer"
android:summary = "#string/pref_sync_noti_timer_summ"
android:dialogMessage = "#string/pref_sync_noti_timer">
</com.example.test.TimerForNoti>
and this is class of dialog preference (TimerForNoti) :
public class TimerForNoti extends DialogPreference
{
public TimerForNoti(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.time_picker);
setPositiveButtonText(R.string.ok);
setNegativeButtonText(R.string.cancel);
Dialog di = new Dialog(context);
di.setContentView(R.layout.time_picker);
OnClickListener test = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tag = v.getTag().toString();
if(tag == "1"){
Log.v("onOne", "ok");
}
// .....
}
};
Button btn1 = (Button) di.findViewById(R.id.button1);
btn1.setOnClickListener(test);
}
}
If I understand your question right, you can set tag for each button and set one OnClickListener for all of them:
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="6"
android:background="#android:color/transparent" />
Code:
OnClickListener buttonsListener = new OnClickListener() {
#Override
public void onClick(View v) {
String buttonText = v.getTag().toString();
// your logic here
}
};
Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(buttonsListener);
Button btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(buttonsListener);
Button btn3 = (Button)findViewById(R.id.button3);
btn3.setOnClickListener(buttonsListener);
// your buttons here
Also you can use v.getId() in OnClickListener for determining which button was clicked
As much I understood your question. You want one method to be called on every button clicked.
For that Add this to your every button element in xml:
<Button
android:onClick="OnClick"
.. />
And this method to your .java file where you want to perform task:
public void OnClick(View v){
// do something....
}
Or you can directly call setOnClickListener at button object
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// do something....
}
});
I have been trying for a while to work out how to dynamically create a RelativeLayout with multiple views inside (e.g. TextView, ProgressBar) a LinearLayout to create a RelativeLayout beneath the previous one after every button click. Please can anyone look at my code and see if there is anything that I can do to solve this issue.
Here is the code:
activity_test_container.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frag1ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/testLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".TestContainerActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/testContainerTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/testContainerTextView1"
android:layout_marginBottom="16dp"
android:text="TextView2" />
<TextView
android:id="#+id/testContainerTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="TextView1" />
<Button
android:id="#+id/testContainerButton1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/testContainerTextView2"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
container.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginBottom="16dp"
android:background="#color/display_panels" >
<ProgressBar
android:id="#+id/containerProgressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/containerImageButton2"
android:max="100"
android:progress="40" />
<TextView
android:id="#+id/containerTextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/containerTextView6"
android:layout_alignLeft="#+id/containerProgressBar1"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/containerTextView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/containerProgressBar1"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageButton
android:id="#+id/containerImageButton2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/containerTextView6"
android:background="#color/display_panels"
android:contentDescription="Okay icon"
android:src="#drawable/ic_green_ok" />
</RelativeLayout>
TestContainerActivity.java
public class TestContainerActivity extends Activity implements OnClickListener {
LinearLayout containerLayout;
Button testButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_container);
testButton = (Button)findViewById(R.id.testContainerButton1);
containerLayout = (LinearLayout)findViewById(R.id.testLayout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_container, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==testButton){
createNewLayout();
}
}
public void createNewLayout(){
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View addView = layoutInflater.inflate(R.layout.container, null);
containerLayout.addView(addView);
}
}
I'm not entirely sure what you're problem is, but I suspect it's that the rows are not showing up at all because I don't see where you attach the listener to the Button. To handle a click event, an OnClickListener needs to be set on your View. Though this is commonly done with Buttons, OnClickListeners can be set on any view, so any size/shape widget can be made clickable. This is done with the setOnClickListener method of a View. There are multiple ways to do this, try modifying your onCreate like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_container);
testButton = (Button)findViewById(R.id.testContainerButton1);
containerLayout = (LinearLayout)findViewById(R.id.testLayout);
testButton.setOnClickListener(this);
}
An alternative method to setting your listener would be to create the listener in onCreate rather than using the Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_container);
testButton = (Button)findViewById(R.id.testContainerButton1);
containerLayout = (LinearLayout)findViewById(R.id.testLayout);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNewLayout();
}
});
}
In this case, you wouldn't need to have your Activity implement OnClickListener. I usually only will do something like that if I have many buttons with similar functionality, where creating listeners for each will cause a performance hit. For more isolated cases like this, I prefer to set individual Listeners since the performance difference will be negligible, but that's just my personal preference.
Hope this helps! If your problem was actually based somewhere else, please modify your question and I'll try my best to assist! Also, keep in mind that you can use the Log class to post information about execution in your LogCat output. It really helps with debugging! I suspect that if you put some logging in your listener and createNewLayout() right now, you'd see that the logging never happens because those methods are never called.