I am using two buttons, one for next news and one for previous news. In my activity when I am on the first news my button should set the image with the grey image and when I move forward my previous news button should change from the grey image to the colored one. Similarly when I reach at the last news my next button should change to grey image.
This is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF"
>
<TextView
android:id="#+id/tv_submitDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textColor="#000000"
android:padding="5dp"/>
<TextView
android:id="#+id/tv_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_submitDate"
android:text="Medium Text"
android:textColor="#000000"
android:textStyle="bold"
android:padding="5dp"/>
<View
android:id="#+id/viewSeperator"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_below="#+id/tv_headline"
android:background="#FF909090" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_relative_layout"
android:layout_below="#+id/viewSeperator" android:padding="10dp">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_detailNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textColor="#000000" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/btn_relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#DCDCDC"
android:padding="10sp" >
<Button
android:id="#+id/btn_prevNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_weight="0.5"
android:layout_marginRight="5sp"
android:background="#drawable/arrow_left"
android:clickable="true" />
<Button
android:id="#+id/btn_nextNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_marginLeft="5sp"
android:layout_weight="0.5"
android:background="#drawable/arrow_right"
android:clickable="true" />
</LinearLayout>
</RelativeLayout>
This is where I am trying to change the image of my button:
public void onClick(View v) {
if (v == btnPrevNews && newsId > 0) {
if (newsId > 0) {
newsId--;
putDetailNewsinView(newsId);
} else if (newsId == 0) {
btnPrevNews
.setBackgroundResource(R.drawable.disable_arrow_left);
btnPrevNews.setEnabled(false);
}
// btnPrevNews.setVisibility(View.INVISIBLE);
} else if (v == btnNextNews && newsId < newsListDetail.size() - 1) {
if (newsId < newsListDetail.size() - 1) {
newsId++;
putDetailNewsinView(newsId);
if(newsId == 0)
btnNextNews.setBackgroundDrawable(getResources().getDrawable(
R.drawable.disable_arrow_right));
} else if (newsId == newsListDetail.size()) {
// btnNextNews.setVisibility(View.INVISIBLE);
btnNextNews.setBackgroundDrawable(getResources().getDrawable(
R.drawable.disable_arrow_right));
// btnNextNews.setBackgroundResource(R.drawable.disable_arrow_right);
btnNextNews.setEnabled(false);
}
}
}
Please help me out in this.
Thanks in advance.
I assume you have Methods which will be executed if the Button is pressed. Just use these and add Logic to change the Background of these Buttons to grey:
Give both Buttons an ID, you can access these Buttons with:
Button mybutton = (Button) findViewById(R.id.yourid);
Now you can do all kinds of things with your button, like changing the background.
edit:
The Problem could lay in the Pictures, but if i were you, i would set some breakpoints and debug step-by-step.
I hacked some code of mine to test this functionality, it works fine for me. Here is it:
package my.namespac;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SomeTestProjectActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.btn_prevNews);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button)v;
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.koala));
}
});
}
}
And the Layout-Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn_prevNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_weight="0.5"
android:layout_marginRight="5sp"
android:background="#drawable/ic_launcher"
android:clickable="true" />
</LinearLayout>
Related
Iam Developing an android app in which one of its module is of Quiz.I want to show Tick image on button when someone click the right answer and wrong when wrong ans is seleceted, I want to show these tick and cross images on the right most of my button.Right now Iam displaying the toast for right and wrong answer.Also I want to show the next quiz question on the same activity when user click the next or previous button.I dont want to create another activity for next question rather want to show the next question on the same activity.
the sample image like what I want to do is like this :
enter image description here
Code of Quiz xml is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/empty"
tools:context=".activity.QuizActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10sdp"
android:orientation="vertical">
<TextView
android:id="#+id/quiz_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Quiz"
android:textStyle="bold"
android:textSize="#dimen/_30ssp"
android:textColor="#FFF"
android:gravity="center_horizontal"/>
<TextView
android:layout_below = "#id/quiz_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Which was the first non test playing country to beat India in an international match?"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginRight="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/afghan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Afghanistan"
android:textStyle="bold"
android:textColor="#fff"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:textSize="15sp"
android:background="#drawable/button_border"/>
<Button
android:layout_below = "#id/afghan"
android:id="#+id/bang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Bangladesh"
android:textStyle="bold"
android:textColor="#fff"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:textSize="15sp"></Button>
<Button
android:id="#+id/srilanka"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Srilanka"
android:textStyle="bold"
android:textColor="#fff"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:textSize="15sp"
android:background="#drawable/button_border"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:background="#drawable/left_arrow"
android:layout_gravity="bottom"
android:layout_margin="#dimen/_20sdp"/>
<ImageView
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:background="#drawable/right_arrow"
android:layout_gravity="bottom"
android:layout_marginBottom="#dimen/_20sdp"
android:layout_marginLeft="#dimen/_150sdp"/>
</LinearLayout>
</LinearLayout>
Code for Activity is :
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.vshine.neuron.riseshine.R;
public class QuizActivity extends AppCompatActivity {
Button btn1, btn2, btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
btn1 = findViewById(R.id.afghan);
btn2 = findViewById(R.id.bang);
btn3 = findViewById(R.id.srilanka);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Sorry! Wrong Answer",Toast.LENGTH_LONG).show();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Sorry! Wrong Answer",Toast.LENGTH_LONG).show();
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Congratulation! Right Answer",Toast.LENGTH_LONG).show();
}
});
getSupportActionBar().setTitle("Quiz");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish(); // close this activity and return to preview activity (if there is any)
}
return super.onOptionsItemSelected(item);
}
}
I'd suggest you do couple of things.
Instead of hardcodding your buttons' onclick methods, try determining which button was clicked and was it the write answer or not.
You can make a method which will check if the clicked button holds the right answer or not.
For reusing the activity, you can simply update the textview (that holds the question) and the button's texts.
I was wondering why a button I made to show/hide my layouts only works once, i.e, initially my layouts are GONE, then when I click a button they're VISIBLE, but later when I click the same button, their View is not being set back to GONE.
/**
* Method to show/hide buttons, on button click.
* #param v
*/
public void hideOrDisplayOptionIconsButton(View v)
{
// Hide layouts if VISIBLE
if(mMapViewsButtonsLinearLayout.getVisibility() == View.VISIBLE
&& mLocationButtonsLinearLayout.getVisibility() == View.VISIBLE)
{
mMapViewsButtonsLinearLayout.setVisibility(View.GONE);
mLocationButtonsLinearLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
mMapViewsButtonsLinearLayout.setVisibility(View.VISIBLE);
mLocationButtonsLinearLayout.setVisibility(View.VISIBLE);
}
}
Here is a sample which should work for you
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout mMapViewsButtonsLinearLayout=null;
private LinearLayout mLocationButtonsLinearLayout=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapViewsButtonsLinearLayout= (LinearLayout) findViewById(R.id.mMapViewsButtonsLinearLayout);
mLocationButtonsLinearLayout= (LinearLayout) findViewById(R.id.mLocationButtonsLinearLayout);
}
public void hideOrDisplayOptionIconsButton(View v)
{
// Hide layouts if VISIBLE
if(mMapViewsButtonsLinearLayout.getVisibility() == View.VISIBLE
&& mLocationButtonsLinearLayout.getVisibility() == View.VISIBLE)
{
mMapViewsButtonsLinearLayout.setVisibility(View.GONE);
mLocationButtonsLinearLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
mMapViewsButtonsLinearLayout.setVisibility(View.VISIBLE);
mLocationButtonsLinearLayout.setVisibility(View.VISIBLE);
}
}
}
Here is the layout
<?xml version="1.0" encoding="utf-8"?>
<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="com.hideshow.MainActivity">
<LinearLayout
android:id="#+id/mMapViewsButtonsLinearLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
<LinearLayout
android:id="#+id/mLocationButtonsLinearLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button4" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide or Show"
android:id="#+id/hideorshow"
android:onClick="hideOrDisplayOptionIconsButton" />
</LinearLayout>
</LinearLayout>
I have an OnClickListener that listens to Button A clicks. I also have 2 TextViews below the Button.
I want that on every click on Button A, the 2 TextViews will switch their places between them, like this:
Before clicking:
TextView A
TextView B
After clicking:
TextView B
TextView A
How can I do it? Is there a special function that meant to do that? or some kind of a trick? Thanks!
actvity_main.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="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
</RelativeLayout>
MainActivity.java:
txtA = (TextView) findViewById(R.id.txtA);
txtB = (TextView) findViewById(R.id.txtB);
float mAX = txtA.getX();
float mAY = txtA.getY();
float mBX = txtB.getX();
float mBY= txtB.getY();
txtA.setX(mBX);
txtA.setY(mBY);
txtB.setX(mAX);
txtB.setY(mAY);
Trick is changing the x and y axis of both views :
Your xml code:
<?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="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:text="A"
android:tag="A"
android:clickable="true"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA"
android:text="B"
android:tag="B"
android:clickable="true"
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch"
android:id="#+id/btn1"
android:onClick="onClickButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Java Code :
public class MainActivity extends Activity {
TextView txtA,txtB;
boolean _isOnTxtA;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (TextView)findViewById(R.id.txtA);
txtB = (TextView)findViewById(R.id.txtB);
btn1 = (Button)findViewById(R.id.btn1);
}
public void onClickButton(View v)
{
float mPos1x,mPos1y,mPos2x,mPos2y;
mPos1x = txtB.getX();
mPos1y = txtB.getY();
mPos2x = txtA.getX();
mPos2y= txtA.getY();
if(_isOnTxtA)
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA = false;
}
else
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA= true;
}
}
}
View.bringToFront method may be useful.
Where your layout is like:
<LinearLayout>
<TextView A>
<TextView B>
</LinearLayout>
To call bringToFront on TextView A will bring it front (the last position in the parent LinearLayout).
<LinearLayout>
<TextView B>
<TextView A>
</LinearLayout>
For more detailed example, below is layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/text_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_a" />
<TextView
android:id="#+id/text_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_b" />
</LinearLayout>
In your OnClickListener (in your Activity) call:
View textViewA = findViewById(R.id.text_a);
textViewA.bringToFront();
This should work.
Toggling behavior can be achieved by this application. For example:
ViewGroup textHolder = (ViewGroup) findViewById(R.id.text_holder);
textHolder.getChildAt(0).bringToFront();
ViewGroup.getChildAt(0) always returns the first child of the ViewGroup. So everytime you call bringToFront on the first child will be bring to front.
All previous answers do not work because you use a relative layout where a linear layout will suffice.
If you have to go RelativeLayout, do the following in your click listener
TextView textA = (TextView) findViewById(R.id.txtA);
TextView textB = (TextView) findViewById(R.id.txtB);
RelativeLayout.LayoutParams textALayoutParams = (RelativeLayout.LayoutParams) textA.getLayoutParams();
textALayoutParams.addRule(RelativeLayout.BELOW, R.id.txtB);
textA.setLayoutParams(textALayoutParams);
RelativeLayout.LayoutParams textBLayoutParams = (RelativeLayout.LayoutParams) textB.getLayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textBLayoutParams.removeRule(RelativeLayout.BELOW);
} else {
textBLayoutParams.addRule(RelativeLayout.BELOW,0);
}
textB.setLayoutParams(textBLayoutParams);
This will place A under B. You can do the same for reversing.
I have a problem concerning setting a listener that would execute different action based on the clicked button.
The compiler doesn't detect any error on the syntax, but the problem is that when I simulate my app on the emulator, it pops up the window asking for "Force Close".
Here is my Java file :
package tp.imc.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class IMCCalculatorActivity extends Activity {
EditText poids,taille;
Button boutton1,boutton2;
TextView text1;
Boolean k=true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
poids=(EditText) findViewById(R.id.poids);
taille=(EditText) findViewById(R.id.taille);
boutton1 =(Button) findViewById(R.id.boutton1);
text1=(TextView) findViewById(R.id.text1);
boutton1.setOnClickListener(clicker);
boutton2.setOnClickListener(clicker);
}
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener clicker = new View.OnClickListener(){
public void onClick (View v){
if( v == boutton1 )
{
String a,b;
Double r;
a=poids.getText().toString();
b=taille.getText().toString();
Double zero=Double.parseDouble(b);
a=poids.getText().toString();
b=taille.getText().toString();
if (zero==0)
{
text1.setText("Erreur ! Division par 0.");
}
else {
r=(Double.parseDouble(a))/(Double.parseDouble(b)*Double.parseDouble(b));
if (r<16.5) text1.setText("Famine.");
else if (r>16.5 && r<18.5) text1.setText("Maigreur.");
else if (r>=18.5 && r<25) text1.setText("Coplulence nomrale.");
else if (r>=25 && r<30) text1.setText("Surpoids.");
else if (r>=30 && r<35) text1.setText("Obésité modérée.");
else if (r>=35 && r<40) text1.setText("Obésité sévère.");
else if (r>=40) text1.setText("Obésité morbide ou massive.");
}
}
else if( v == boutton2 )
{
poids.setText("");
taille.setText("");
text1.setText("");
}
}
};
}
And here is my XML file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/poids"
android:textColor="#FF0000"
android:gravity="center"
/>
<EditText
android:id="#+id/poids"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/poids"
android:lines="1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/taille"
android:textColor="#FF0000"
android:gravity="center"
/>
<EditText
android:id="#+id/taille"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/taille"
android:lines="1"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/metre"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/centimetre"
/>
</RadioGroup>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mega"
android:checked="false"
/>
<Button
android:id="#+id/boutton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/compute" />
<Button
android:id="#+id/boutton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/raz" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/resultat"
/>
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/text"
/>
</LinearLayout>
You don't define boutton2, it is null. Add this:
boutton2 = (Button) findViewById(R.id.boutton2);
boutton2.setOnClickListener(clicker);
Since boutton1 and boutton2 don't have common code between them you could simply create two different listeners.
Lastly, this line doesn't look right.
if( v == boutton1 )
You are trying to compare a View to a Button which may not work. Try this:
if( v.getId() == boutton1.getId() )
The main page has a horizontalScrollView across the top and a number of TextView text fields within it. I would like to be able to scroll and click on any on the TextView text and it change the RelativeLayout to match the selection made in the HorizontalScrollView.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<HorizontalScrollView android:id="#+id/horizontalScrollView1" android:layout_height="wrap_content" android:scrollbars="none" android:layout_width="wrap_content">
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent" android:id="#+id/linearLayout1">
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTOne" android:text="tOne"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTTwo" android:text="tTwo"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTThree" android:text="tThree"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFour" android:text="tFour"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFive" android:text="tFive"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSix" android:text="tSix"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSeven" android:text="tSeven"></TextView>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/relativeLayout1"></RelativeLayout>
</LinearLayout>
I have a second layout page called tone which would be linked to the menu option tOne in the HorizontalScrollView.
tone.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">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is tOne"></TextView>
</LinearLayout>
The main activity will flag if one of the first 4 options are selected by writing to the LogCat. Is it possible to display the tone.xml within the relativelayout if the user clicks the tOne option from the HorizontalScrollView? Then if the user selects tTwo display ttwo.xml in the RelativeLayoutView etc?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SlideMenu extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView tOne, tTwo, tThree, tFour, tFive, tSix, tSeven, tEight, tNine, tTen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout vSpace = (RelativeLayout)findViewById(R.id.relativeLayout1);
tOne = (TextView)findViewById(R.id.EditTOne);
tTwo = (TextView)findViewById(R.id.EditTTwo);
tThree = (TextView)findViewById(R.id.EditTThree);
tFour = (TextView)findViewById(R.id.EditTFour);
tFive = (TextView)findViewById(R.id.EditTFive);
tSix = (TextView)findViewById(R.id.EditTSix);
tSeven = (TextView)findViewById(R.id.EditTSeven);
tOne.setOnClickListener(this);
tTwo.setOnClickListener(this);
tThree.setOnClickListener(this);
tFour.setOnClickListener(this);
tFive.setOnClickListener(this);
tSix.setOnClickListener(this);
tSeven.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
//--------------
case R.id.EditTOne:
System.out.println("Text One pressed");
break;
case R.id.EditTTwo:
System.out.println("Text Two pressed");
break;
case R.id.EditTThree:
System.out.println("Text Three pressed");
break;
case R.id.EditTFour:
System.out.println("Text Four pressed");
break;
}
}
}
I believe it is fairly simple. I added
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.tone, vSpace);
after you print "Text One Pressed". Be sure to make vSpace a field first.
To do this with all of them, call
vSpace.removeAllViews()
before you inflate each view.