A lot activity in one layout - android

I am developing Restaurant menu app. I have 9 image button in activity_main.xml. For example: If when i clicked soup image button, the app will soup price and soup image in other layout. But i dont want create 9 layouts file for this. All of prices appear one layout. I didnt write method for this. I am waiting your help. Sorry for my bad english.
This is my menu.
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton2"
android:layout_gravity="left|top"
android:background="#drawable/corbalar"
android:contentDescription="#string/corbalar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton3"
android:layout_gravity="center_horizontal|top"
android:background="#drawable/zeytinyaglilar"
android:contentDescription="#string/zeytinyaglilar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton4"
android:layout_gravity="right|top"
android:background="#drawable/spesiyaller"
android:contentDescription="#string/spesiyaller"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton5"
android:layout_gravity="left|center_vertical"
android:background="#drawable/pideler"
android:contentDescription="#string/pideler"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton6"
android:layout_gravity="center"
android:background="#drawable/salatalar"
android:contentDescription="#string/salatalar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton7"
android:layout_gravity="right|center_vertical"
android:background="#drawable/kebaplar"
android:contentDescription="#string/kebaplar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton8"
android:layout_gravity="left|bottom"
android:background="#drawable/tatlilar"
android:contentDescription="#string/tatlilar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton9"
android:layout_gravity="center_horizontal|bottom"
android:background="#drawable/mesrubatlar"
android:contentDescription="#string/mesrubatlar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton10"
android:layout_gravity="right|bottom"
android:background="#drawable/sicakicecekler"
android:contentDescription="#string/sicakicecekler"
android:clickable="true"
android:onClick="degistirActivity"/>
This is the place I'll show information.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="tr.edu.fatih.nad0.com.kilislimenu.fiyatGoster">
//my items image
<ImageButton
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageButton11"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/gosterilecekyemek" />
//my items price
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:id="#+id/textView"
android:layout_below="#+id/imageButton11"
android:layout_centerHorizontal="true" />

What you'll want to do is use an intent with extras to pass information of the clicked item to the activity. There's a couple of ways to do this:
1) Manually create an OnClickListener for each item (ideally, you'd generate your items from a SQLite database rather than having a static list).
Using an OnClickListener would look something like this:
ImageButton myButton = (ImageButton) findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent priceIntent = new Intent(getApplicationContext(), priceActivity.class);
priceIntent.putExtra("price",6.99);
priceIntent.putExtra("name","Hamburger");
startActivity(priceIntent);
}
});
Then in your onCreate method for priceActivity.class, you'd read the values like this:
Bundle extras = getIntent().getExtras();
double price = extras.getDouble("price");
String name = extras.getString("name");
2) Use the onClick attribute in your XML to process the information through an identifying tag or some such.
In your XML you can specify android:onClick="myMethod" as well as an identifying tag, like android:tag="hamburger". Then you'd make your myMethod:
public void myMethod(View v) {
String tagId = v.getTag().toString();
// Create intent with extras like above
}
Again, ideally you'd be loading all of this information from a database so that you can easily access the information with fewer methods rather than manually entering the hard-coded data straight into the app.

you can follow below link to workout with your problem.
http://developer.android.com/training/basics/firstapp/starting-activity.html
Alternatively,
you can implement method like this on your button.
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
On another Activity, you can receive Intent and Display your price.
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);

Related

Filling a layout dynamically crashes the application without errors

When I try to open this new activity through a button on a previous layout the app crashes with no apparent reason.
My code is this :
public class Main3Activity extends AppCompatActivity {
private int limitsImages[] = {R.drawable.onelimits, R.drawable.twolimits, R.drawable.threelimits, R.drawable.fourlimits, R.drawable.fivelimits, R.drawable.sixlimits,
R.drawable.sevenlimits, R.drawable.eightlimits, R.drawable.ninelimits, R.drawable.tenlimits};
private String answersLimits[] = {"-9", "2", "1/10", "1/6", "1/2", "12", "1", "2/3", "5/6", "1/4"};
private String choicesLimits[][] = {{"-19", "19", "-9", "2"},{"0", "-2", "Does not exist", "2"}, {"Does not exist","1/5","0","1/10"},{"1/6","1/5","1/4","1"}
,{"1","1/2","0","Does not exist"},{"0","4","12","10"},{"0","1","1/2","2/3"},{"2/3","1","3/2","0"},{"1","2/6","5/6","4"},{"1/4","1"
,"Does not exist","4"}};
private TextView questionNum;
private TextView questionTxt;
private ImageView questionImage;
private CheckBox choiceOne;
private CheckBox choiceTwo;
private CheckBox choiceThree;
private CheckBox choiceFour;
private static int testIdentifier=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
questionNum = (TextView) findViewById(R.id.questionNumber);
questionTxt = (TextView) findViewById(R.id.questionText);
questionImage = (ImageView) findViewById(R.id.questionImage);
choiceOne = (CheckBox) findViewById(R.id.choice1);
choiceTwo = (CheckBox) findViewById(R.id.choice2);
choiceThree = (CheckBox) findViewById(R.id.choice3);
choiceFour = (CheckBox) findViewById(R.id.choice4);
//choicesLimits[4][3]=getString(R.string.doesntexist);
//choicesLimits[9][2]=getString(R.string.doesntexist);
startLimitsQuestion(testIdentifier);
}
public void startLimitsQuestion( int questionNum) {
this.questionNum.setText(questionNum+1+"/10");
questionTxt.setText(getString(R.string.limitsQ));
questionImage.setImageResource(limitsImages[questionNum]);
choiceOne.setText(choicesLimits[questionNum][1]);
choiceTwo.setText(choicesLimits[questionNum][2]);
choiceThree.setText(choicesLimits[questionNum][3]);
choiceFour.setText(choicesLimits[questionNum][4]);
}
Sub Question:I have been troubled a bit if my initialization and declaration before the onCreate() method is correct.
edit: Layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/activity_main2"
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"
android:background="#color/colorPrimaryDark"
tools:context="com.example.learnmath.Main2Activity"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button4"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"
android:layout_marginTop="27dp"
android:layout_below="#+id/textView3"
android:layout_alignLeft="#+id/button5"
android:layout_alignStart="#+id/button5"
android:text="#string/quadratics" />
<Button
android:text="#string/derivatives"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button6"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="18dp"
android:layout_marginEnd="18dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/timetostart"
android:id="#+id/imageView"
android:layout_alignTop="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="22dp"
android:layout_above="#+id/button6" />
<Button
android:text="#string/limits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button5"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"
android:layout_marginTop="35dp"
android:layout_below="#+id/button4"
android:layout_alignLeft="#+id/button6"
android:layout_alignStart="#+id/button6"
android:elevation="0dp"
android:onClick="limitsQuiz" />
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
app:srcCompat="#drawable/mathtest"
android:id="#+id/imageView2"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/button6"
android:layout_alignEnd="#+id/button6" />
<TextView
android:text="#string/tests"
android:typeface="monospace"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_orange_light"
android:id="#+id/textView3"
android:textSize="35sp"
android:shadowColor="#android:color/background_light"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/imageView2"
android:layout_alignStart="#+id/imageView2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/index"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/imageView3"
android:onClick="homePage" />
</RelativeLayout>
You said that you clicked and then it crashed. So some problem will be there in the onclick. We need to follow your code from onclick. The above snippet is just showing the first question display and these are correct except the test identifier is not incremented.
Please do post full class code of that snippet. Also do give logcat error
What i could guess is some where the error should be in registering activity to manifest or error durimg onclick where your accessing arrays. May be arrayoutofbounds too.
I have resolved your problem. The app theme you declared in your AndroidManifest.xml is not appropriate. As you are extending the AppCompatActivity in your activity, you need to use the Appcompat theme for your particular activity or application.
Change your current app theme to android:theme="#style/Theme.AppCompat.Light" in AndroidManifest.xml file will resolve your problem.
Note:: Your initialisation and declaration is perfect and your onClick method is not the issue. This AppTheme is the only issue. Tell me if you didn't get me.
Use Firebase crash library to report Crash to detect crash if you are not able to crash in logcate
https://firebase.google.com/docs/crash/

Android AutoCompleteTextView return wrong string

I have a little problem in my android app. I want to put a word in an AutoCompleteTextView and then jump to a specific Activity(depend on the word that the user gave).
The problem is that, when i give a specific word, the program does not correspond with the correct answer as expected but returns a wrong toast message. However with the log that i put i see the correct answer. I think that the solution is silly but i stuck and need to solve this.
the code:
Activity.java
public class paralActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paral);
final String [] temp = {"one","two","three"};
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.clearListSelection();
final RelativeLayout myRelative = (RelativeLayout) findViewById(R.id.find);
myRelative.setVisibility(View.INVISIBLE);
ImageView myImage = (ImageView) findViewById(R.id.aktoploika);
myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(paralActivity.this, topParalActivity.class);
startActivity(intent);
}
});
ImageView myOtherImage = (ImageView) findViewById(R.id.aeroporika);
myOtherImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myRelative.setVisibility(View.VISIBLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(paralActivity.this, android.R.layout.select_dialog_item, temp);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.RED);
ImageView findBeach = (ImageView) findViewById(R.id.find_beach);
findBeach.setOnClickListener(new View.OnClickListener() {
#Override
** public void onClick(View view) {
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
String choice = actv.getText().toString();
Log.i("Answer",choice);
if (choice == "one"){
Intent firstIntent = new Intent(paralActivity.this, nauagioActivity.class);
startActivity(firstIntent);
}else if (choice == temp[1]){
Intent secondIntent = new Intent(paralActivity.this, gerakasActivity.class);
startActivity(secondIntent);
}else if (choice == temp[2]){
Intent thirdIntent = new Intent(paralActivity.this, limnionasActivity.class);
startActivity(thirdIntent);
}else{
Toast.makeText(paralActivity.this,"wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
To help u understand, in this activity a have 2 imageViews. When the user presses the second, a relativeLayout appears (with an AutoCompleteTextView and a button inside). After the user writes the word when presses the button it must go to the specific activity. I declared a String Array (temp[3]) with three words inside and 3 activities for each of the words.
The problem starts in the last onclick method that i put ** . Every time i put a correct word from the Array i take the Toast message but in the log i see the correct.
here is Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_paral"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/main"
android:scaleX="2"
tools:context="com.example.billy.zakynthosapp.paralActivity">
<TextView
android:id="#+id/categories"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/welcome"
android:text="#string/categories"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="11sp"
android:textStyle="bold"
android:textColor="#color/welcome"
android:textAllCaps="true"/>
<ImageView
android:id="#+id/aktoploika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="#id/categories"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="#drawable/par1" />
<TextView
android:id="#+id/aktoploika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aktoploika"
android:layout_alignTop="#+id/aktoploika"
android:layout_alignRight="#+id/aktoploika"
android:layout_alignBottom="#+id/aktoploika"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/paralies"
android:textSize="22sp"
android:textColor="#color/categories"
android:textStyle="bold"/>
<ImageView
android:id="#+id/aeroporika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="#id/aktoploika"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="#drawable/par2" />
<TextView
android:id="#+id/aeroporika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aeroporika"
android:layout_alignTop="#+id/aeroporika"
android:layout_alignRight="#+id/aeroporika"
android:layout_alignBottom="#+id/aeroporika"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/search"
android:textSize="22sp"
android:textColor="#color/categories"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/aeroporika"
android:layout_marginTop="50dp"
android:layout_centerInParent="true"
android:background="#color/categories"
android:scaleX="0.5">
<TextView
android:id="#+id/textView_1"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:text="#string/find_paral"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/find_beach"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="200dp"
android:src="#drawable/find"
android:onClick="find"/>
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView_1"
android:layout_marginTop="10dp"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
Can anyone help me with this?
Thanks a lot!
Try choice.equals("one") instead of choice == "one"

Characters appear in the order as i click

For example. There is picture on the top of the screen below that there are some empty boxes and below the boxes there are some buttons. Every button has a character for text("a","c","t"). You click on a button and the button's text appear in the box. You can click them in the order you want to but the answer is "cat" so when you put the characters in the correct order then you got a toast.
I tried to do it with TextViews and Buttons. I can make the button disappear when i click on it and a textview appear in the same time. But every textview has a fix place on the screen, so i need to put every character in every box invisible and when i click on the "c" character it appear in the first box and the other "c" characters stay invisible. But if i click on the "a" first, then it appears in the second box because there is too much variation to do all. I'm not good at explaining but if anyone has an idea how to do that easier please response!
Here is my code:
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button b1;
Button b2;
Button b3;
TextView tg1;
TextView tg2;
TextView tg3;
TextView to1;
TextView to2;
TextView to3;
TextView tl1;
TextView tl2;
TextView tl3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.bg);
b1.setOnClickListener(this);
b2 = (Button)findViewById(R.id.bo);
b2.setOnClickListener(this);
b3 = (Button)findViewById(R.id.bl);
b3.setOnClickListener(this);
tg1 = (TextView)findViewById(R.id.tg1);
tg2 = (TextView)findViewById(R.id.tg2);
tg3 = (TextView)findViewById(R.id.tg3);
to1 = (TextView)findViewById(R.id.to1);
to2 = (TextView)findViewById(R.id.to2);
to3 = (TextView)findViewById(R.id.to3);
tl1 = (TextView)findViewById(R.id.tl1);
tl2 = (TextView)findViewById(R.id.tl2);
tl3 = (TextView)findViewById(R.id.tl3);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.bg:
b1.setVisibility(View.INVISIBLE);
tg1.setVisibility(View.VISIBLE);
tg2.setVisibility(View.INVISIBLE);
tg3.setVisibility(View.INVISIBLE);
break;
case R.id.bo:
b2.setVisibility(View.INVISIBLE);
to2.setVisibility(View.VISIBLE);
to1.setVisibility(View.INVISIBLE);
to3.setVisibility(View.INVISIBLE);
break;
case R.id.bl:
b3.setVisibility(View.INVISIBLE);
tl3.setVisibility(View.VISIBLE);
tl2.setVisibility(View.INVISIBLE);
tl1.setVisibility(View.INVISIBLE);
}
}
}
<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="hu.szada.gombokelso.MainActivity"
android:orientation="horizontal">
<TextView
android:id="#+id/tl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
android:text="l"/>
<Button
android:id="#+id/bo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="86dp"
android:onClick="onClick"
android:text="o" />
<Button
android:id="#+id/bl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/bg"
android:layout_alignBottom="#+id/bg"
android:layout_alignParentLeft="true"
android:layout_marginLeft="36dp"
android:onClick="onClick"
android:text="l" />
<Button
android:id="#+id/bg"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/bo"
android:onClick="onClick"
android:text="g" />
<TextView
android:id="#+id/tg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/to1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
/// Second
<TextView
android:id="#+id/to2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl1"
android:layout_alignBottom="#+id/tl1"
android:layout_marginLeft="19dp"
android:layout_toRightOf="#+id/tl1"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
<TextView
android:id="#+id/tg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignLeft="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
/// Third
<TextView
android:id="#+id/tg3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/bl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_alignLeft="#+id/tg3"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
<TextView
android:id="#+id/to3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_toRightOf="#+id/tl3"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
You might want to try a slightly different approach.
If I understand you correctly, you want to "type" a word out using given lettered buttons. Like one of those hangman style games.
Why not append the text views on the fly.
Something like
#Override
public void onClick(View v) {
//Grab the surrounding layout for the textviews
GridView answerGrid = (GridView)getViewById(R.id.answerGrid);
//Get the text that was on the button
Button b = (Button)v;
String btnText = b.getText().toString();
//Make a text view with text
TextView txt = new TextView();
text.setText(btnText);
//Append to text view container
answerGrid.addView(txt);
//Invisible button
b.setVisibility(View.INVISIBLE);
}
Haven't tested to see if this is perfect, but its a start.
=====
I've looked at your xml
Why not use GridViews?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
....>
<GridView android:id="#+id/answerGrid"
....>
<!-- Put nothing here. This is for answers -->
</GridView>
<GridView android:id="#+id/lettersGrid"
android:layout_below="answerGrid"
....>
<!-- Buttons in here -->
</GridView>
</RelativeLayout>
This way you can customise the number of rows/columns based on the length of the word you're playing with. And GridView will automatically give you a neat layout and spacing.
Have a look at the GridView doc and get it customised the way you want it.
See my edits above for the Java code.

Passing values to another activity via intent

I am trying to pass some values via intent and show them in a new activity. I have tried many solutions but the problem is always the same: There are two textviews in new activity and when I set the text via code but only the textview tv2 appears.
Please help. I am new to android and java and trying to learn the concepts for a new app.
MainActivity.java:
Intent intent = new Intent(MainActivity.this,Details.class);
intent.putExtra(NAME, name);//name is string type
intent.putExtra(VALUE, value);//value is long type
startActivity(intent);
Details.java :
TextView tv1 = (TextView) findViewById(R.id.name);
TextView tv2 = (TextView) findViewById(R.id.value);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.NAME);
Long value=intent.getLongExtra(MainActivity.VALUE, 0);
String value_text=value.toString();
tv1.setText(name);
tv2.setText(value_text);
Layout For Details.java(Image is for further experiments):
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="40sp" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp" />
</LinearLayout>

android.widget.imagebutton cannot be cast to android.widget.button Message

Hi I am getting this message android.widget.imagebutton cannot be cast to android.widget.button (See attachment pic for logcat error) which I don't understand why? because my code seems to be right.
Code:
ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar);
Calendar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent OpenCalendarbtn = new Intent (Menu.this, DisplayCalendar.class);
startActivity (OpenCalendarbtn);
}
});
Complete Layout xml file:
`
<ImageButton
android:id="#+id/Read"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/Write"
android:layout_marginRight="31dp"
android:src="#drawable/read" />
<ImageView
android:id="#+id/AppLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/logo" />
<ImageButton
android:id="#+id/Write"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/AppLogo"
android:layout_marginLeft="35dp"
android:layout_marginTop="14dp"
android:src="#drawable/write" />
<ImageButton
android:id="#+id/Calendar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignLeft="#+id/Write"
android:layout_below="#+id/Write"
android:layout_marginTop="20dp"
android:src="#drawable/calendar/>
`
Make sure you have used the <ImageButton> tag instead of <Button> tag in your layout resource file.
That could be the cause of the error.
Change your
ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar);
To:
Button Calendar = (Button) findViewById (R.id.Calendar);
Its an Eclipse problem not in your code.
Solution for this is just copy your code in xml by Ctrl+c and save the xml then again paste it again then save it and run.....this works with me.

Categories

Resources