Add a RelativeLayout dynamically to a LinearLayout in Android - android

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.

Related

Accessing element from same layout parent

i have a listview with different components (2 linear layouts and a button) , what i want to do is when i click on a button that's inside one of those linear layouts , i want to access a textview that's inside the other linearlayout , is it possible ?
<?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"
android:orientation="vertical">
<ListView
android:id="#+id/listV2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="85dp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/nom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/textCname"
android:textSize="21dp"
android:textStyle="bold" />
<TextView
android:id="#+id/numero"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#color/textCother"
android:textStyle="italic" />
<TextView
android:id="#+id/ville"
android:layout_width="match_parent"
android:textColor="#color/textCother"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textStyle="italic" />
</LinearLayout>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:visibility="invisible" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
>
<ImageView
android:id="#+id/remove"
android:onClick="contactRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/edit"
android:onClick="contactEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"/>
<ImageView
android:onClick="contactCall"
android:id="#+id/call"
android:layout_marginLeft="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
what i want to do is access the value of android:id="#+id/numero"
when i click on one of the image views
You can set OnClickListener interface provided by View class. Something like this.
Implement listener on your activity class
public class SpinnerActivity extends AppCompatActivity implements
View.OnClickListener
Declare class level variables for your views
private TextView tvNumero;
private ImageView ivRemove, ivEdit, ivContactCall;
Find initialise value in onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gildi_spinner);
tvNumero = (TextView) findViewById(R.id.numero)
ivRemove = (ImageView) findViewById(R.id.remove);
ivEdit = (ImageView) findViewById(R.id.edit);
ivContactCall = (ImageView) findViewById(R.id.contactCall);
ivRemove.setOnClickListener(this);
ivEdit.setOnClickListener(this);
ivContactCall.setOnClickListener(this);
}
Implemented class from Onclickistener, where you get on click event for your registered views
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.remove:
case R.id.edit:
case R.id.contactCall:
Toast.makeText(this, tvNumero.getText().toString(), Toast.LENGTH_SHORT).show();
break;
}
}
NOTE : tvNumero.getText().toString() gives you the value of your desired TextView.
UPDATE :
Firstly replace your ListView with RecyclerView
Refer RecyclerView Example tutorial.
Secondly to achieve onClick for your listed items
Refer recyclerview-onclick tutorial.
Pass context when you create your adapter, Use that context to get the inflated view.
Adapter adapter = new Adapter(this);
Then in Adpater Class constructor :
public Adapter(Context context){
context.findViewById(R.id.textview);//consider this as numero textview
}
Then access it as per your requirement. Hope it helps!

Android Button receives OnTouch event but not OnClick

I have the following layout in which I'm trying to register an OnClickListener for the button list_item_setup_step_button_start. the Button receives touch events but no click events. any help is appreciated.
<RelativeLayout
android:id="#+id/list_item_setup_step_layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/list_item_setup_step_label"
android:layout_width="#dimen/list_item_setup_state_label_width"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#color/activity_setup_step_state_next_color"
android:clickable="false"/>
<FrameLayout
android:id="#+id/list_item_setup_step_frame_layout_progress"
android:layout_width="#dimen/list_item_setup_step_progress_bar_size"
android:layout_height="match_parent">
<com.mikhaellopez.circularprogressbar.CircularProgressBar
android:id="#+id/list_item_setup_step_progress_bar"
android:layout_width="40dp"
android:layout_height="40dp"
app:cpb_background_progressbar_color="#FFCDD2"
app:cpb_progressbar_color="#F44336"
android:visibility="invisible"
android:layout_gravity="center_vertical|center_horizontal"/>
<Button
android:id="#+id/list_item_setup_step_button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:padding="#dimen/text_margin_normal"
android:background="#color/color_accent_default"
android:textColor="#color/text_color_light"
android:visibility="invisible"
android:clickable="true"
android:textSize="#dimen/text_size_normal"
android:layout_marginLeft="#dimen/text_margin_normal"/>
</FrameLayout>
<LinearLayout
android:layout_toLeftOf="#id/list_item_setup_step_label"
android:layout_toRightOf="#id/list_item_setup_step_frame_layout_progress"
android:layout_marginRight="#dimen/text_margin_normal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/list_item_setup_step_text_view_title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="16sp"
android:gravity="right|center_vertical"
android:textColor="#color/text_color_light"/>
<TextView
android:id="#+id/list_item_setup_step_text_view_status"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:textColor="#color/text_color_light" />
</LinearLayout>
</RelativeLayout>
this is the code for the listener (I have a custom ScrollView which ignores touch events so that it can only be scrolled programmatically), the 'Clicked' log never appears and the functions aren't called.
private void handleStartButton() {
currentStepStartButton = (Button) stepViews[currentPhase][currentStep].findViewById(R.id.list_item_setup_step_button_start);
currentStepStartButton.setTypeface(BaseActivity.getFont(this));
final TextView status = (TextView) stepViews[currentPhase][currentStep].findViewById(R.id.list_item_setup_step_text_view_status);
progressBar = (CircularProgressBar) stepViews[currentPhase][currentStep].findViewById(R.id.list_item_setup_step_progress_bar) ;
progressBar.setProgress(0);
StyleHelper.applyStyle(this, progressBar);
status.setTypeface(BaseActivity.getFont(this));
if(currentStep == 0 && currentPhase == 0) {
status.setText(getString(R.string.activity_setup_press_button_to_begin));
currentStepStartButton.setText(getString(R.string.commons_start));
} else {
status.setText(getString(R.string.activity_setup_press_button_to_continue));
currentStepStartButton.setText(getString(R.string.commons_start));
}
currentStepStartButton.setVisibility(View.VISIBLE);
currentStepStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e(Constants.DEBUG_TAG, "Clicked") ;
currentStepStartButton.setVisibility(View.INVISIBLE);
handleCurrentStepAction();
}
});
}
EDIT:
The following method was being called before the method which set the listener:
private void requestPermissions() {
int currentAPIVersion = android.os.Build.VERSION.SDK_INT ;
if (currentAPIVersion >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
android.Manifest.permission.SEND_SMS,
android.Manifest.permission.RECEIVE_SMS
}, 1);
}
}
Once this method was removed, the OnClickListener started working again. I don't really have any idea why. Any theories ?
There seem to be some interference from your other code (which is not visible in this question) since the button click method works if the code you provided, except for those parts which are not visible, is inserted into a new project.
Java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
final Button currentStepStartButton = (Button) findViewById(R.id.list_item_setup_step_button_start);
currentStepStartButton.setVisibility(View.VISIBLE);
currentStepStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("MainActivity", "Clicked");
currentStepStartButton.setVisibility(View.INVISIBLE);
}
});
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/list_item_setup_step_layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/list_item_setup_step_frame_layout_progress"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/list_item_setup_step_button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="invisible"
android:clickable="true"/>
</FrameLayout>
</RelativeLayout>
</LinearLayout>
Try to temporarily disable unnecessary features for the button to work to zero in on the issue.
Well I faced the same problem while working on another project and figured what was really wrong here.
The problem, as suggested in the question itself, rises from the use of requestPermissions method. Although calling this method in case the permission its trying to acquire is already granted might seem to not show a dialog over the current activity, some invisible overlay seems to be added anyway which also appears to be clickable.
The solution hence is to always check if one already has the permissions one is trying to acquire and only fire up requestPermissions in case those permissions are not granted yet.

Using TextView to inflate Layout

Upon clicking each TextView they should lead to another layout file to help the user learn about human trafficking. In the onCreate method I have setOnClickListener to my text view. Inflating it is an issue though. Is this called inflating a view? I've seen people recommending the use of fragments for this, using setContentView (from what I've found this shouldn't be used), and using the layout inflater while passing in the layout I want and null. However that doesn't work. How should this code look?
The XML:
<?xml version="1.0" encoding="utf-8"?>
<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: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="com.example.piatt.worksafe.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work Safe!"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is Human Trafficing?"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/whatIsHumanTrafficing"
android:clickable="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I get safe labor?"
android:layout_below="#+id/whatIsHumanTrafficing"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/howDoIGetSafeLabor"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I check that my job / job offer is legal?"
android:layout_below="#+id/howDoIGetSafeLabor"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:gravity="center"
android:id="#+id/checkLegality"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How can I get help?"
android:layout_below="#+id/checkLegality"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/getHelp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About us"
android:layout_below="#+id/getHelp"
android:layout_centerHorizontal="true"
android:textSize="16sp"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:id="#+id/aboutUs"
/>
</RelativeLayout>
The java class:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView whatIsHumanTrafficing = (TextView) findViewById(R.id.whatIsHumanTrafficing);
whatIsHumanTrafficing.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
//What is this context, why do I need it and where does it come from?
//What is the ViewGroup, why do I need it and where does it come from?
view.inflate(Context context, R.layout.what_is_human_trafficing, ViewGroup root);
}
});
}
Aside: You could read the documentation for View.inflate
What is this context, why do I need it and where does it come from?
What: YourActivity.this (replace with your actual class name)
Why: You need it as a parameter to inflate a View.
What is the ViewGroup, why do I need it and where does it come from?
You probably don't need it; it can be null. It is the ViewGroup to inflate the View into. Like for a ListView, you would load each row into the list as the "group".
Anyways, if you want to dynamically show different layout file based upon what is clicked, yes Fragment's are one way to do that, and so are ViewStub's.
And you aren't actually calling inflate on the TextView because it is a static method. It is equivalent to doing this with a LayoutInflater
View v = LayoutInflater.from(YourActivity.this).inflate(R.layout.your_layout, null);
// TODO: Do something with v
The final code should look like:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.splashScreenId);
TextView whatIsHumanTrafficing = (TextView) findViewById(R.id.whatIsHumanTrafficing);
whatIsHumanTrafficing.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
MainActivity.this.setContentView(R.layout.what_is_human_trafficing);
}
});
}
Set an on click listener to the text view then within it on MainActivity.this set the content view to the layout you want.

how to display textview items from edittext in listview one by one in another layout?

I am trying to show textview items from entries in edittext on button click to listview in another layout with scrollview.. Code is not running.. application error so help me.
this is my 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: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=".MainActivity" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner1"
android:layout_toRightOf="#+id/editText5"
android:text="#string/add" />
<EditText
android:id="#+id/editText5"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_alignTop="#+id/textView5"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="number" />
<RelativeLayout
android:layout_width="70dp"
android:layout_height="140dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/spinner1"
android:layout_marginTop="17dp" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</ScrollView>
<ListView
android:layout_width="wrap_content"
android:layout_height="125dp">
</ListView>
this is my java code.
private Button mbtn_currencyadd;
private EditText medit_currency;
private TextView mtv1=null;
private ListView mlv;
public String s1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtv1=new TextView(this);
mlv.addView(mtv1);
//mll2.addView(mtv2);
mbtn_currencyadd=(Button)findViewById(R.id.button3);
medit_currency=(EditText)findViewById(R.id.editText5);
mbtn_currencyadd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
s1=medit_currency.getText().toString();
s1=mtv1.getText().toString()+"\n"+s1;
mtv1.setText(s1);
mtv1.setTextSize(17);
medit_currency.setText("");
}
});
Have a look at this example.
It is very similar to what you are looking for.Items are added dynamically from Edit text to list view on button click
You can ask if you have any further queries :)
Of course it won't work. Here's why: -
You list view doesn't have an id
You are not getting a reference to your listview (m1v) in your activity
In order to add items to a listview, you need to create an adapter
See this example http://developer.android.com/guide/topics/ui/layout/listview.html

android onClickListener (newbie)

I copied the following code from a book. And when I run the app, it fails to launch.
The offending line is aboutButton.setOnClickListener(this);. If I knock it out, the app works.
Any clues?
Thanks.
G.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View aboutButton = findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(this);
setContentView(R.layout.activity_main);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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"
android:gravity="center"
tools:context=".MainMenu"
>
<TextView
android:id="#+id/welcome_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="25sp"
android:text="#string/welcome_title"
/>
<Button
android:id="#+id/search_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/search_button"
/>
<Button
android:id="#+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/new_button"
/>
<Button
android:id="#+id/help_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/help_button"
/>
<Button
android:id="#+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit_button"
/>
<Button
android:id="#+id/main_about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/exit_button"
android:layout_alignLeft="#+id/exit_button"
android:text="#string/main_about_button"
android:clickable="true"
/>
</LinearLayout>
aboutButton is null. You can't initialize it until you have inflated the layout. Change it to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Switch these two lines
View aboutButton = findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(this);
}
Your Views exist within your layout which means they will return null if you try to initialize them before inflating your layout, using a layout inflater or by calling setContentView(), which will give you a NPE when trying to call a function on them or setting a listener
had to put the listener statements AFTER the seContentView command. that fixed it.
my guess is, that the setContentView imports all the variables & ids from the layout. and the findViewById just returned a null pointer or something.
if anyone can confirm my guess, i'd appreciate it.
I recommend you to declare the aboutButton as a Button. Try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aboutButton = (Button) findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
});
}
I hope it helps!

Categories

Resources