I added splash.xml and did project - clean which led to losing R.java. The cause was I have an mp3 file in raw folder which was giving error. I deleted that mp3 file and got back the R.java. Now my MainActivity.java is all grayed out. It's like none of the import or the code linked to activity_main.xml. All looks like something I have typed on notepad. Can someone point out what's wrong here?
package com.tenz.secondApp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Yout total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Your total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45sp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Add one"
android:layout_gravity="center"
android:textSize="20sp"
android:id="#+id/bAdd" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Subtract one"
android:layout_gravity="center"
android:textSize="20sp"
android:id="#+id/bSub" />
</LinearLayout>
I think you have multiple packages in your application.Please see your gen folder to verify that R.java in which package.and then
import packagename.R
You need to correct your imports.
Just import R.java from your project
import <YourPkgName>.R;
You have not closed LinearLayout at the beginning properly hence the error. Use the below code. I tested by removing that error, it works perfectly fine..check this
<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45sp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Add one"
android:layout_gravity="center"
android:textSize="20sp"
android:id="#+id/bAdd" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Subtract one"
android:layout_gravity="center"
android:textSize="20sp"
android:id="#+id/bSub" />
</LinearLayout>
Related
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="#+id/away"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="#string/away"
android:gravity="center"
android:textSize="20sp"/>
<TextView
android:id="#+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="#string/_0"
android:textSize="20sp" />
<Button
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="50dp"
android:onClick="score1"
android:text="#string/score" />
<TextView
android:id="#+id/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:text="#string/home"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:id="#+id/score2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="50dp"
android:text="#string/_0"
android:textSize="20sp" />
<Button
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="score2"
android:text="#string/score" />
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reset"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:textColor="#ff1100"/>
</LinearLayout>
I searched it on multiple websites but I still can't the answer. I also tried using the__setText("")it
didn't work as well as the the if() method. I have also tried the intent method also doesn't seem to
work, and many other codes , please help me because I am stuck at this bit , I know that it is simple but
I just can't find it. Thank you
The following codes will set the TextView score to blank when Button one is clicked
First approach
To use the android:onClick="score1" in your XML
Add this to your activity
TextView mTvScore1 = (TextView) findViewById(R.id.score)
public void score1(View v) {
mTvScore1.setText("");
}
Second approach
Button mBtn = (Button) findViewById(R.id.one)
TextView mTvScore1 = (TextView) findViewById(R.id.score)
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTvScore1.setText("");
}
});
Activity code
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTvScore1 = (TextView) findViewById(R.id.score);
Button mBtn = (Button) findViewById(R.id.one);
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score1(v);
}
});
}
public void score1(View v) {
mTvScore1.setText("");
}
}
I'm creating a simple quiz according to this tutorial, but the app crashes with the button "Next answer" which is related to a checked radio button.
Main code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class Display extends Activity {
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton rb1, rb2,rb3;
String questions[]={"Capital of Portugal?", "Capital of Spain?", "Capital of France?"};
String answers[]={"Lisbn","Madrid","Paris"};
String options[]={"Zuriq", "London","Lisbon","Manchester","Buenos Aires","Madrid", "NY", "Paris","Moscow"};
int flag=0;
public static int marks,correct,wrong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dispaly);
tv=(TextView) findViewById(R.id.tvque);
btnNext=(Button) findViewById(R.id.btnNext);
rb1= (RadioButton) findViewById(R.id.radioButton);
rb2= (RadioButton) findViewById(R.id.radioButton2);
rb3= (RadioButton) findViewById(R.id.radioButton3);
tv.setText(questions[flag]);
rb1.setText(options[flag*3]);
rb2.setText(options[(flag*3)+1]);
rb3.setText(options[(flag*3)+2]);
Toast.makeText(this, "Negative Marks: " + MainActivity.tbflag, 1000).show();
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//The error comes from the line bellow, i believe:
RadioButton user_answer= (RadioButton)findViewById(rg.getCheckedRadioButtonId());
String answerText=user_answer.getText().toString();
if(answerText.equalsIgnoreCase(answers[flag])){
correct++;
}else{
wrong++;
flag++;
if(flag<questions.length){
tv.setText(questions[flag]);
rb1.setText(options[flag*3]);
rb2.setText(options[(flag*3)+1]);
rb3.setText(options[(flag*3)+2]);
}
else
{
if(MainActivity.tbflag)
{
marks=correct-wrong;
}
else
{
marks=correct;
}
Intent in =new Intent(getApplicationContext(), ResultActivity.class);
startActivity(in);
}
}
}
});
}
}
Log message:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
I don't understand this error, why isn't the method being called? Can someone help?
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:id="#+id/activity_main"
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.vtorferreira.anew.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvque"
tools:text="Questions" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group">
<RadioButton
android:text="RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp"
android:id="#+id/radioButton" />
<RadioButton
android:text="RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/radioButton2" />
<RadioButton
android:text="RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton3"
android:layout_below="#+id/radioButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RadioGroup>
<Button
android:text="Next Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
android:layout_marginBottom="125dp"
android:id="#+id/btnNext" />
</RelativeLayout>
The reason you are getting the first NullPointerException is because you must inflate rg before it is used.
The second NullPointerException is possibly due to the fact that you are pressing the "Next" button with no radio button selected. To prevent this, you can add a null check inside the OnClickListener or select one of the radio buttons by default. To select a radio button by default, add the following to your XML:
<RadioGroup
...
android:checkedButton="#+id/some-radio-button-id">
...
</RadioGroup>
Alternatively, you can set the default checked RadioButton in your Activity:
rg.check(R.id.radioButton)
I'm new for android java coding. I'm try to do menu list where just have tick box, and once tick the items, n press next, it should go to view layout and show items and the total of the item selected, then press next it should open details page where user must put their details n press send button to send via email. I don't know how to call the items from menu to Cart and to confirmation class.
This is menu.java .
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MenuActivity extends Activity {
Button btnorder;
Button btnback;
Button btnlinkcart;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
btnlinkcart = (Button) findViewById(R.id.button1);
btnback = (Button) findViewById(R.id.button2);
// back button click event
btnback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
}
});
// Link to Cart Screen
btnlinkcart.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ViewActivity.class);
startActivity(i);
finish();
}
});
}
}
This is Cart.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ViewActivity extends Activity {
Button btnconfirm;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
btnconfirm = (Button) findViewById(R.id.button1);
// Link to Cart Screen
btnconfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ConfirmActivity.class);
startActivity(i);
finish();
}
});
}
}
This is cart.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/wallpaper" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Confirm" />
</RelativeLayout>
This is menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wallpaper"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Back" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:text="Pizza (Large) RM30.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox1"
android:layout_marginTop="20dp"
android:text="Pizza (Mediume) RM20.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox2"
android:layout_marginTop="20dp"
android:text="Pizza (Personal) RM10.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox3"
android:layout_marginTop="20dp"
android:text="Chicken Wings RM12.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox4"
android:layout_marginTop="19dp"
android:text="Garlic Bread RM6.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox5"
android:layout_marginTop="20dp"
android:text="Soft Drink (Large) RM5.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox6"
android:layout_marginTop="19dp"
android:text="Soft Drink (Medium) RM4.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Make the variable you want to get in the menu class static, then in the cart class you can get them like this:
menu.variableFromMenuClass
public static ArrayList<YourObject> selectItemList= new ArrayList<YourObject>();
on selectItem(){
//each Item you add here
selectedList.add(selectedObject);
}
on disselectItem(){
//remove if exist in list
selectedList.remove(disselectedOject);
}
sendemail(){
send your public static selecteditem list.
}
This is the algorithm you have to write simple.
I'm new to android development, this is just my second app i built just to kill time, though, i could'nt understand, what caused this error. Need expert advice
The activity_starting_point.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".StatingPoint" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp"
android:text="Enter Number 1"
android:id="#+id/tvDisplay"
/>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp"
android:text="Enter Number 2"
android:id="#+id/tvDisplay"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add"
android:layout_gravity="center" />
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Your Total is 0 "
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
</LinearLayout>
The Java class file: StartingPoint.java
package com.ankur.calulator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class StatingPoint extends Activity {
int num1,num2;
int total;
Button add,sub;
TextView display;
EditText no1,no2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stating_point);
total=0;
no1=(EditText) findViewById(R.id.editText1);
no2=(EditText) findViewById(R.id.editText2);
display=(TextView)findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
total=num1+num2;
display.setText("Your Total is "+total);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
total=num1-num2;
display.setText("Your Total is "+total);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_stating_point, menu);
return true;
}
}
The logcat file:
its got blank, i think i messed up with it, though i saw something as java null pointer exception before
i knw i'm close, just nt able to find some missing link, need advice.
You haven't initialized add and sub like you do for no1,no1 and display
And you are trying to call setOnClickListener on add and sub without initializing so you get a NullPointerException
You need to provide different id to add and sub and initialize it.
In your activity_starting_point.xml, you have:
3 TextViews with the same id: tvDisplay
2 Buttons with the same id: button1
Please assign unique AND meaningful IDs to be able to reference them later in your java code, eg:
android:id="#+id/btAdd"
and
android:id="#+id/btSubtract"
(NOTE: you actually don't need id for TextViews with instructions, eg. all those tvDisplay, unless you want to actually reference them in Java, for example to change the instruction text)
Next, as spotted by Apoorv, whereas you initialize your no1 and no2, you don't do it with add and sub, hence they are null. Just initialise them the same way the same way as the above:
add = (Button) findViewById(R.id.btAdd);
and
sub = (Button) findViewById(R.id.btSubtract);
Also, in your OnClickListeners, your calculations will always yield 0 for total, because nowhere in the code you're actually getting values for num1 and num2. You probably want to do something like:
num1 = Integer.parseInt(no1.getText().toString());
num2 = Integer.parseInt(no2.getText().toString());
I am trying to add an icon to my tabs and it isnt working,
I tried adding
getResources().getDrawable(R.drawable.bodyicon));
and it still isnt working. When I run the application the tab bar just has the text and there is no icon
I outlined where my code is. If someone could figure out why it isnt working that would be great.
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Body extends Activity implements OnTouchListener, OnClickListener {
Button bAbs, bQuad2, bPecs;
TabHost th;
ImageView body;
final Context context = this;
StretchType pop;
// above I am defining all of the buttons and images, etc...
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// here I am saying get rid of the nav bar
setContentView(R.layout.body);
th = (TabHost) findViewById(R.id.thbodyview);
bAbs = (Button) findViewById(R.id.bAbss);
bPecs = (Button) findViewById(R.id.bPecss);
bAbs.setOnClickListener(this);
bPecs.setOnClickListener(this);
//*********************below are my tabs*******************************************
th.setup();
TabSpec specs = th.newTabSpec("Front");
specs.setContent(R.id.Front);
specs.setIndicator("Front",getResources().getDrawable(R.drawable.bodyicon));
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.Back);
specs.setIndicator("Back");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("More...");
th.addTab(specs);
}
//***************************Above are my tabs *******************************
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bAbss:
// below I am creating the alert dialogue
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.abs, null);
// above I am setting the customview of the alert dialogue
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setView(promptsView);
// here I am officially setting the custom layout
// set dialog message
alertDialogBuilder.setTitle("Stretch Type");
// alert dialogue title
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
// above is creating the alert dialogue
final Spinner mSpinner = (Spinner) promptsView
.findViewById(R.id.sSType);
// above is initializing the spinner
/*
* dont need this button final Button mButton = (Button) promptsView
* .findViewById(R.id.bSpinclose);
*/
// reference UI elements from my_dialog_layout in similar fashion
mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());
// show it
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);
break;
}
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
public class OnSpinnerItemClicked implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
switch (pos) {
case (1):
Intent i = new Intent(Body.this, Practice.class);
Body.this.startActivity(i);
break;
}
}
#Override
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
Here is the xml
<TabHost
android:id="#+id/thbodyview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/Front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/llbottomtop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<TextView
android:id="#+id/tvupper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/bAbs"
android:text="Upper"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_centerVertical="true"
android:background="#color/orange" />
<TextView
android:id="#+id/tvlower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:text="Lower"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
</LinearLayout>
<ImageView
android:id="#+id/ivBody"
android:layout_width="210dp"
android:layout_height="430dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/body_front" />
<Button
android:id="#+id/bPecss"
android:layout_width="83dp"
android:layout_height="50dp"
android:layout_above="#+id/bAbss"
android:layout_alignRight="#+id/bAbss"
android:text="Pecs" />
<Button
android:id="#+id/bAbss"
android:layout_width="80dp"
android:layout_height="77dp"
android:layout_alignBottom="#+id/llbottomtop"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:text="Abs" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/Back"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/llbottomtop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<TextView
android:id="#+id/tvupper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/bAbs"
android:text="Upper"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_centerVertical="true"
android:background="#color/orange" />
<TextView
android:id="#+id/tvlower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:text="Lower"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
</LinearLayout>
<ImageView
android:id="#+id/ivBody"
android:layout_width="210dp"
android:layout_height="430dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/body_back" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
My tip is to get access to the tab title textview and set left|top|right|bottom drawable
Example:
//to get first tab title as textView from tabHost
TextView tabTitle = (TextView) tabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
//to set the icon to left of the tab title
tabTitle.setCompoundDrawablesWithIntrinsicBounds(
getActivity().getResources().getDrawable(
R.drawable.ic_tab_icon), null, null, null);
I have two physical devices one 2.2 and the other 4.2.2. on 2.2 there is no issue and the icon appears. on 4.2.2 there is no icon. Somethings changed in how tabs are laid out in Android. If you really required to use a TabHost i saw you can pass in a view as the tabindicator. This code is kinda pseudo code as it still some adjustment in the UI in terms of layout but try this:
define a layout called tabindicator_layout.xml and put it in res/layout. the contents of which look like this:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:scaleType="center" >
</ImageView>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
then in your class code at runtime inflate this view and populate the image and textview with what you desire. do this for each tab:
//*********************below are my tabs*******************************************
th.setup();
TabSpec specs = th.newTabSpec("Front");
specs.setContent(R.id.Front);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tabindicator_layout, th.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.title)).setText("Front");
((ImageView) tabIndicator.findViewById(R.id.image))
.setImageResource(R.drawable.ic_launcher);
specs.setIndicator(tabIndicator);
TabSpec specs2 = th.newTabSpec("tag2");
specs2.setContent(R.id.Back);
View tabIndicator2 = LayoutInflater.from(this).inflate(
R.layout.tabindicator_layout, th.getTabWidget(), false);
((TextView) tabIndicator2.findViewById(R.id.title)).setText("Back");
((ImageView) tabIndicator2.findViewById(R.id.image))
.setImageResource(R.drawable.ic_launcher);
specs2.setIndicator(tabIndicator2);
TabSpec specs3 = th.newTabSpec("tag3");
specs3.setContent(R.id.tab3);
View tabIndicator3 = LayoutInflater.from(this).inflate(
R.layout.tabindicator_layout, th.getTabWidget(), false);
((TextView) tabIndicator3.findViewById(R.id.title)).setText("...More");
((ImageView) tabIndicator3.findViewById(R.id.image))
.setImageResource(R.drawable.ic_launcher);
specs3.setIndicator(tabIndicator3);
th.addTab(specs);
th.addTab(specs2);
th.addTab(specs3);
of course replace R.drawable.ic_launcher with whatever icon you want to show
another interesting thing that might help..i notice if i pass in a blank for the title that the original way works but shows no title. So you could go into photoshop and just add the title to the image and make it even better looking. Try this way:
TabSpec specs = th.newTabSpec("Front");
specs.setContent(R.id.Front);
specs.setIndicator("",getResources().getDrawable(R.drawable.ic_launcher));
th.addTab(specs);