I'm very new to this. My buttons weren't responding to clicks. I added android:onClick="onClick" to the xml file. Now i get the error below.
java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class testapp.two.MainActivity for onClick handler on view class android.widget.Button with id 'buttonMinus'
Please help, java code along with the manifest file and main xml are included below.
Thanks.
package testapp.two;
import testapp.two.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button okButton, minusButton, plusButton;
TextView textScore, scoreCard;
int score = 95;
private static final String TAG = "GolfScore";
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate started");
okButton = (Button)findViewById(R.id.buttonOK);
minusButton = (Button)findViewById(R.id.buttonMinus);
plusButton = (Button)findViewById(R.id.buttonPlus);
textScore = (TextView)findViewById(R.id.textScore);
scoreCard = (TextView)findViewById(R.id.scoreCard);
//set button listeners
okButton.setOnClickListener(this);
minusButton.setOnClickListener(this);
plusButton.setOnClickListener(this);
textScore.setText(String.valueOf(score));
Log.d(TAG, "onCreate finished");
}//onCreate
#Override
public void onClick(View v) {
Log.d(TAG, "in onCreate");
switch (v.getId()){
case R.id.buttonMinus:
score--;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonPlus:
score++;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonOK:
break;
}//end of switch
}//end of my onclick
}//end of MainActivity
<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:onClick="#+id/buttonMinus, #+id/buttonPlus, #+id/textScore"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:text="Golf Score App"
android:textAlignment="center" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:layout_marginTop="14dp"
android:baselineAligned="false"
android:gravity="fill"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonMinus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:clickable="true"
android:onClick="onClick"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="-" />
<Button
android:id="#+id/buttonPlus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:clickable="true"
android:onClick="onClick"
android:layout_weight="1"
android:text="+" />
<TextView
android:id="#+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="92"
android:textSize="20sp" />
<Button
android:id="#+id/buttonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:layout_marginRight="33dp"
android:clickable="true"
android:onClick="onClick"
android:layout_weight="1"
android:text="Ok"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="#+id/scoreCard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="22dp"
android:text="Score card info goes here" />
</RelativeLayout>
As 0xDEADC0DE says, you don't need the OnClickListener if you use the onClick attribute in your XML and vice versa.
The reason why the onClick(View v) method can't be found is the Overrideannotation. It depends to the OnClickListener this way. There is a "free" method without annotation needed that the method can be found if the onClick tag is used.
Now you have two options: get rid of the OnClicklistener and the association to the buttons or the onClick in your XML.
Edit:
Remove the onClick attributes from the RelativeLayout and the Buttons in your XML. In addition, remove clickable from your Buttons. If you set clickable = true, the View will get an empty OnClickListener automatically. This means your OnClickListener in your Activity will not be called.
Just remove android:onClick="onClick" from your XML.
Note that it is preferrable to make a single OnClickListener for each button rather than one big OnClickListener that has to check the id to decide what to do.
Related
I need to perform a task whereby a user need to click on multiple buttons to perform a single task for example dialog box, image or toast.
I mean that clicking on more than one button in order to execute a task. It would be better to let the user know he or she has already clicked on the first button by highlight the button with a color. And when he or she click on the second or third button,highlight the button and it will execute the task like toast or dialog box .
Can someone assist me thanks.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.android.testing1.MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="104dp"
android:layout_marginTop="20dp"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="34dp"
android:text="Button2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_centerVertical="true"
android:text="Button3" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button3"
android:layout_below="#+id/button3"
android:layout_marginTop="34dp"
android:text="Button4" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button4"
android:layout_alignParentBottom="true"
android:layout_marginBottom="24dp"
android:text="Button5" />
</RelativeLayout>
package com.example.android.testing1;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import static android.R.id.button1;
import static android.R.id.button2;
import static android.R.id.button3;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4,b5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button dodo = (Button) findViewById(button1);
Button dede = (Button) findViewById(button2);
Button dada = (Button) findViewById(button3);
Button didi = (Button) findViewById(button4);
Button hehe = (Button) findViewById(Button5);
If(dodo == true, dede==true, dada == true , didi=true, hehe=true);
Then perform same task ike toast or dialog box after click on all the five buttons
I want to create a custom keyboard inside a custom dialog.
It has a Textview on top and 12 buttons bellow (it's a numerical keyboard)
What I want is: when the buttons are pressed they update the Textview values.
I don't know the right place to put the "OnClickListener" and how to update the Textview...
Here is the keyboard layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:id="#+id/valor"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"/>
<Button
android:id="#+id/btn8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"/>
<Button
android:id="#+id/btn9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"/>
<Button
android:id="#+id/btn5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"/>
<Button
android:id="#+id/btn6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"/>
<Button
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"/>
<Button
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"/>
<Button
android:id="#+id/btnP"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."/>
<Button
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok"/>
</TableRow>
And here is the MainActivity:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button teclado = (Button)findViewById(R.id.teclado);
teclado.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
showCustomDialog();
}
});
}
public void showCustomDialog()
{
final Dialog dialog = new Dialog(this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.teclado);
dialog.show();
final TextView valor = (TextView) dialog.findViewById(R.id.valor);
Button btn7 = (Button)dialog.findViewById(R.id.teclado);
btn7.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
valor.setText("Test");
}
});
}
}
Thanks in advance!!
Sorry, I found this implementation strange.
I would suggest using DialogFragment, and you can encapsulate everything in there.
http://android-developers.blogspot.ca/2012/05/using-dialogfragments.html
You will be able to use the normal setOnClickListener in that fragment.
Button btn7 = (Button)dialog.findViewById(R.id.btn7); **"why R.id.teclado?"**
btn7.setOnClickListener(this)
And your fragment can implement OnClickListener with switch
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn7:
valor.setText("asdf");
break;
case R.id.btn8:
...
break;
....
}
// TODO Auto-generated method stub
}
Ok I want to give some basic idea regarding this. you can modify this according to your requirement.
Give your layout an id. and use layoutinflater to inflate your xml in dialog.
If you want your xml layout in your dialog box you you can do it like this:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.your_layout_id_here, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();
Put above code in Button's onclicklistener. And check Button's view through Switch case like case 1: "this button id" then insert this number in Text view.
Hope this help you
R.id.teclado is not in the dialog layout
Change:
Button btn7 = (Button)dialog.findViewById(R.id.teclado);
To:
Button btn7 = (Button)dialog.findViewById(R.id.btn7);
I am new to app making, and am making a simple calculator.
Whenever I click a button, I get the message "Unfortunately, Calculator has stopped." Any help would be greatly appreciated.
XML File:
<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=".Calculator" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="70dp"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginLeft="70dp"
android:layout_marginTop="26dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Add"
android:onClick="DoAdd" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="90dp"
android:text="Subtract"
android:onClick="DoMinus"/>
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/button2"
android:onClick="DoTimes"
android:text="Multiply" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="94dp"
android:text="TextView" />
Java File:
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator, menu);
return true;
}
public void doAdd(View v)
{
EditText firstNumber = (EditText)findViewById(R.id.editText1);
EditText secondNumber = (EditText)findViewById(R.id.editText2);
double x = Double.parseDouble(firstNumber.getText().toString());
double y = Double.parseDouble(secondNumber.getText().toString());
double total = x + y;
TextView answerSpace = (TextView)findViewById(R.id.textView1);
answerSpace.setText(Double.toString(total));
}
}
Thankyou.
Change your xml onClick attribute to exactly match the method name in your java code:
<Button
android:id="#+id/button1"
...
android:onClick="doAdd" />
Notice the lower-case d at the beginning.
If those two don't match, the app crashes while Android is trying to invoke the onClick method you specified.
FD_ already pointed out the mistake from your code.
But for better coding approach you can follow this. If you wish.
In your xml file, define single method name for onClick on each button ie for
addition,substration,division,multiplication.
What i mean to say...?
// For Addition....
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Add"
android:onClick="calculate" />
//Define android:onClick="calculate" for remaining 3 buttons
ie say for subtraction
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Subract"
android:onClick="calculate" />
do similarly for division and multiplication
Now, in Java File
1)Define Variables globally
2)Find their ids in onCreateMethod(), ie when the activity created.
How to do this?
public class Calculator extends Activity
{
EditText firstNumber,secondNumber;
onCreate()
{
firstNumber = (EditText)findViewById(R.id.editText1);
secondNumber = (EditText)findViewById(R.id.editText2);
}
public void calculate(View v)
{
switch(v.getId())
{
case R.id.button1:
// Do coding here addition....
break;
case R.id.button2:
// Do coding here substraction....
break;
case R.id.button3:
// Do coding here multiplication....
break;
case R.id.button4:
// Do coding here division....
break;
}
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
runtime exception ListView whose id attribute is ‘android.R.id.list’
I am new to android I get the following runtime error.
"Your content must have a ListView whose id attribute is android.R.id.list"
I was trying following tutorial
http://www.mkyong.com/android/android-listview-example/
"Custom ArrayAdapter example"
when I run their code it works fine and they use android 2.3.3
I use 4.0
I changed their code according to my requriment and got above runtime error in logcat.
here is my code.
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.xxxx.xxx.adapter.ListArrayAdapter;
public class Page5SubActivity extends ListActivity {
static final String[] MOBILE_OS = new String[] { "Android", "iOS",
"WindowsMobile", "Blackberry", "test", "test2" };
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.page5sub);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent intent = getIntent();
int val = intent.getIntExtra("id", 0);
switch (val) {
case 2:
setListAdapter(new ListArrayAdapter(this, MOBILE_OS));
break;
case 3:
break;
case 4:
break;
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
public void onClickBtn(View v) {
switch (v.getId()) {
case R.id.back_arrow:
Intent intent2 = new Intent(Page5SubActivity.this,
Page5Activity.class);
// intent2.putExtra("id", 2);
startActivity(intent2);
break;
}
}
}
Here is the code of 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="#drawable/page1background"
android:gravity="center" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:layout_marginBottom="#dimen/padding_Xlarge"
android:layout_marginTop="#dimen/padding_large"
android:paddingTop="#dimen/padding_large"
android:text="#string/text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/textbody" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_below="#id/textView1"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#id/linearLayout1"
android:weightSum="10" >
<View
android:id="#+id/view1"
android:layout_width="0dip"
android:layout_height="30dp"
android:layout_marginBottom="35dp"
android:layout_weight="1" />
<ImageView
android:id="#+id/logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:contentDescription="#string/Description" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label" >
</TextView>
<View
android:id="#+id/view2"
android:layout_width="0dip"
android:layout_height="30dp"
android:layout_marginBottom="35dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:weightSum="10" >
<View
android:id="#+id/view3"
android:layout_width="0dip"
android:layout_height="30dp"
android:layout_marginBottom="35dp"
android:layout_weight="4.5" />
<ImageButton
android:id="#+id/back_arrow"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="35dp"
android:layout_weight="1"
android:background="#drawable/backbut"
android:contentDescription="#string/Description"
android:onClick="onClickBtn"
android:src="#drawable/backarrowpress" />
<View
android:id="#+id/view4"
android:layout_width="0dip"
android:layout_height="30dp"
android:layout_marginBottom="35dp"
android:layout_weight="4.5" />
</LinearLayout>
</RelativeLayout>
in their example they dont use listview widget in xml file but mention in the log cat.
apartfrom that it says unable to start activity compnonetinfo.
where do I have done wrong?
plz help me to correct this.
Here is the original code of the xml file.
here is the source of original code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/logo"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
android:src="#drawable/windowsmobile_logo" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="30px" >
</TextView>
</LinearLayout>
I notice they dont have use listview widget in their xml file but works this example.
Your solution in your error
"Your content must have a ListView whose id attribute is android.R.id.list"
If your are using ListActivity then you must have ListView in your xml of layout and must be id of ListView is android.R.id.list
So must add listview in your layout like below code
<ListView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/android:list" />
when you extend ListActivity then your xml layout file must have ListView whose id is android.R.id.list
so put listview in xml layout file and android:id="#+id/android.R.id.list"
in your layout xml file you must set id of you ListView to #android:id/list
set your Listview id like this
android:id="#id/android:list"
set adapter like this.
Adapter = new Adapter<String>(this,android.R.layout.simple_list_item_1, list);
or check XML
<ListView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/android:list" />
How to make this kind of tab in Android [http://i.stack.imgur.com/rIbUX.png]
just mention that see the overlap area, when you click on of the tab, the overlap changed.
here is one chinese version example in which I think that's what I want to have, but they don't provide the complete code, and I have no idea how to go on this project.
http://www.oschina.net/question/54100_29061
I am currently using Android 3.2 and ICS 4+ to implement this project, so welcome if that's possible to implement competible with Framgment.
I finally I made the solution by myself. I just leave the native tab host implementation, make up my own.
here is the screen shot:
[http://i.stack.imgur.com/1mXLZ.png]
here is the Activity code:
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class PelesysTabHostActivity extends Activity {
private ImageView ib1, ib2, ib3, last;
private Drawable pressed, released;
private TextView tv,tv1,tv2,tv3;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ib1 = (ImageView) this.findViewById(R.id.imageView1);
ib2 = (ImageView) this.findViewById(R.id.imageView2);
ib3 = (ImageView) this.findViewById(R.id.imageView3);
tv = (TextView) this.findViewById(R.id.textView1);
tv1= (TextView) this.findViewById(R.id.textView11);
tv2= (TextView) this.findViewById(R.id.textView22);
tv3= (TextView) this.findViewById(R.id.textView33);
pressed = getResources().getDrawable(R.drawable.ui_table_tab_button);
released = getResources().getDrawable(
R.drawable.ui_table_tab_button_disabled);
ib1.setImageDrawable(pressed);
last = ib1;
ib1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
last.setImageDrawable(released);
ib1.setImageDrawable(pressed);
last = ib1;
tv.setText("I am super 1");
ib1.bringToFront();
tv1.bringToFront();
}
});
ib2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
last.setImageDrawable(released);
ib2.setImageDrawable(pressed);
last = ib2;
tv.setText("TWO!!! I am super 2");
ib2.bringToFront();
tv2.bringToFront();
}
});
ib3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
last.setImageDrawable(released);
ib3.setImageDrawable(pressed);
last = ib3;
tv.setText(" III !!! I am super 3");
ib3.bringToFront();
tv3.bringToFront();
}
});
}
}
here is the layout file I used (main.xml)
<?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" >
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="330dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ui_table_bg_coursedetail" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_marginLeft="326dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ui_table_tab_button_disabled" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:src="#drawable/ui_table_tab_button_disabled" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="180dp"
android:src="#drawable/ui_table_tab_button_disabled" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_marginLeft="50dp"
android:text="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
<TextView
android:id="#+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView2"
android:layout_marginLeft="50dp"
android:text="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
<TextView
android:id="#+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView3"
android:layout_marginLeft="50dp"
android:text="3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
</RelativeLayout>
Just a week now I used this : https://github.com/AdilSoomro/Iphone-Tab-in-Android
Very well worked! You can customize it as its in your first picture