I have built an application in android using Tab Layout and it has 2 tabs. I want a different button on each of these tabs. But if I define button in main.xml, I get same button on both the tabs.
I even tried defining the buttons separately in the class of each tab, but was getting some weird errors. can somebody please help me out with this. Below is my code:
FinalProj.java:
package FinalProj.com;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class FinalProj extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, iFallApp.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
For Tab1 - iFallApp.java
package FinalProj.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class iFallApp extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the iFall tab");
setContentView(textview);
}
}
Tab 2 - Settings.java
package FinalProj.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Settings extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Settings tab");
setContentView(textview);
}
}
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
I want to have one button along with TextView in iFallApp.java and one button and editText in Settings.java.
For anyone else finding this question:
public class FinalProj extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
The setContentView(R.layout.main) tells the activity what to display on its UI. In this case the view is being set to load from R.layout.main which corresponds to /layout/main.xml (R being the generated lookup class generated by the android framework).
Changing the main.xml would change both tabs because both tabs are contained /within/ the FinalProj activity inside the TabHost view defined inside of main.xml here:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
The whole tab system is described in full detail here:
Tab Layout | Android Developers
Related
how can I add a logo in the actionbar. My main_activity extends Activity. I know the way if I extend ActionBarActivity, but I have to do it with the Activity.
Use This code . You can custom icon (actionbar_logo).
<style name="MyTheme_ActionBar" parent="#style/Theme.AppCompat.Light">
<item name="icon">#drawable/actionbar_logo</item>
</style>
Import this class .
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActivityBarActivity {
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
}
I tested it and it is working.
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#f00fcd">
</android.widget.Toolbar>
</LinearLayout>
MainActivity.java
import android.annotation.TargetApi;
import android.widget.Toolbar;
import android.os.Build;
import android.os.Bundle;
public class MainActivity extends Activity{
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
setActionBar(toolbar);
// setActionBar(toolbar);
toolbar.setLogo(R.drawable.logo);
}
}
I have a very simple app which is just an activity with a tab view on it.
I have initialised and casted everything to as it should be but am continually getting a null pointer error which always links back to
tabHost.setup();
I am using android studio and am new to java. This question has been asked a lot on here but all answers just say to include the setup() and I have already done that.
Here is my .java file:
package com.example.app;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.TabHost;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec1, spec2, spec3;
spec1 = tabHost.newTabSpec("spec1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Tab1");
tabHost.addTab(spec1);
spec2 = tabHost.newTabSpec("spec2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Tab2");
tabHost.addTab(spec2);
spec3 = tabHost.newTabSpec("spec3");
spec3.setContent(R.id.tab3);
spec3.setIndicator("Tab3");
tabHost.addTab(spec3);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
The only difference between my code and some online tutorials is the public class MainActivity extending to ActionBarActivity and not just Activity. I didn't even do this, it was default when i created a blank project.
The XML file is below also:
<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="com.example.app.MainActivity$PlaceholderFragment">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
This Line shows that the TabHost you have created is in fragment_main.xml inside layout directory which is used by PlaceholderFragment
tools:context="com.example.app.MainActivity$PlaceholderFragment"
But you are finding your TabHost in activity_main.xml
Move your code from onCreate() to onCreateView of PlaceholderFragment, below in same class if you are using default template comes with AS like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TabHost tabHost = (TabHost) rootView.findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec1, spec2, spec3;
spec1 = tabHost.newTabSpec("spec1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Tab1");
tabHost.addTab(spec1);
spec2 = tabHost.newTabSpec("spec2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Tab2");
tabHost.addTab(spec2);
spec3 = tabHost.newTabSpec("spec3");
spec3.setContent(R.id.tab3);
spec3.setIndicator("Tab3");
tabHost.addTab(spec3);
return rootView;
}
Or If you want you can move your TabHost to activity_main.xml as well without changing the java code but that is not recommended using fragments having a lot of benefits.
Check this for benefits of using Fragments
fragment_main and activity_main layouts in Android Studio
This my code to use TabHost,but now I use fragment instead.
public class MainActivity extends TabActivity {
private TabHost tabHost;
private TabHost.TabSpec spec;
#SuppressWarnings("unused")
private Resources res;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_new);
//Test Button
Button testBtn = (Button)findViewById(R.id.title_imagebtn);
testBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog();
}
});
this.res = getResources();
this.tabHost = getTabHost();
Intent intent = new Intent().setClass(this, LearnResActivity.class);
this.spec = this.tabHost
.newTabSpec(getString(R.string.res_learn))
.setIndicator(
getString(R.string.res_learn),
getResources().getDrawable(
android.R.drawable.ic_media_play))
.setContent(intent);
this.tabHost.addTab(this.spec);
this.tabHost = getTabHost();
intent = new Intent().setClass(this, MyLearnActivity.class);
this.spec = this.tabHost
.newTabSpec(getResources().getString(R.string.my_learn))
.setIndicator(
getString(R.string.my_learn),
getResources().getDrawable(
android.R.drawable.ic_menu_recent_history))
.setContent(intent);
this.tabHost.addTab(this.spec);
this.tabHost = getTabHost();
intent = new Intent().setClass(this, MyTestActivity.class);
this.spec = this.tabHost
.newTabSpec(getString(R.string.my_test))
.setIndicator(
getString(R.string.my_test),
getResources().getDrawable(
android.R.drawable.ic_menu_edit))
.setContent(intent);
this.tabHost.addTab(this.spec);
}}
I am having a problem when debugging apps that respond to user input. An ordinary app that just displays text works fine, but when I give a button an event handler I get a generic message "Unfortunately, 'App Name' has stopped.". I have tried many solutions, but to no avail. I am using IntelliJ IDEA 12.0.4 (Community Edition).
Sorry for asking a very common question but it seems the solution is different for everyone.
Edit - new working code
Source Code:
package com.example.Android_Test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import static com.example.Android_Test.R.*;
public class MyActivity extends Activity {
EditText editText;
TextView textView;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Declare all widget controls.
editText = (EditText) findViewById(R.id.txtEdit);
Button button = (Button) findViewById(R.id.btnChange);
textView = (TextView) findViewById(R.id.lblText);
button.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(editText.getText());
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtEdit" android:layout_gravity="left|center_vertical"
android:layout_alignParentLeft="true" android:layout_marginLeft="0dp" android:layout_alignParentTop="true"
android:layout_marginTop="0dp" android:layout_alignParentRight="true" android:hint="Type here change text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Text"
android:id="#+id/btnChange"
android:layout_alignLeft="#+id/txtEdit" android:layout_below="#+id/txtEdit"
android:layout_alignRight="#+id/txtEdit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change this text"
android:id="#+id/lblText"
android:layout_alignLeft="#+id/txtEdit" android:layout_below="#+id/btnChange"
android:layout_alignRight="#+id/txtEdit" android:textSize="18dp"/>
</RelativeLayout>
Logcat is very long so I pasted it here: http://pastebin.com/2qaY8ZJj.
Everyone's Logcat is so much shorter i don't know why
Move this code
// Declare all widget controls.
EditText editText = (EditText) findViewById(R.id.txtEdit);
Button button = (Button) findViewById(R.id.btnChange);
TextView textView = (TextView) findViewById(R.id.lblText);
inside onCreate() and use R.id... .
Edit
I edited youre code
public class MyActivity extends Activity {
EditText editText;
TextView textView;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Declare all widget controls.
editText = (EditText) findViewById(R.id.txtEdit);
Button button = (Button) findViewById(R.id.btnChange);
textView = (TextView) findViewById(R.id.lblText);
button.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(editText.getText().toString);
}
};
}
I want to make the tabbar of Android to look like Android.
Here is my code:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost"
android:orientation="vertical" >
<TabWidget
android:layout_width="fill_parent"
android:layout_height="40px"
android:id="#android:id/tabs"
android:layout_alignParentBottom="true"
></TabWidget>
</TabHost>
TabActivity.java
package com.saa;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabActivity extends android.app.TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost=getTabHost();
Intent intent = new Intent();
TabSpec tabSpec= tabHost.newTabSpec("one");
tabSpec.setIndicator("One", getResources().getDrawable(R.drawable.ic_launcher));
intent.setClass(this, tab1.class);
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);
tabSpec= tabHost.newTabSpec("two");
tabSpec.setIndicator("Two", getResources().getDrawable(R.drawable.ic_launcher));
intent = new Intent().setClass(this, tab2.class);
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);
tabSpec= tabHost.newTabSpec("three");
tabSpec.setIndicator("Three", getResources().getDrawable(R.drawable.ic_launcher));
intent = new Intent().setClass(this, tab3.class);
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);
tabSpec= tabHost.newTabSpec("four");
tabSpec.setIndicator("Four",getResources().getDrawable(R.drawable.ic_launcher));
intent = new Intent().setClass(this, tab4.class);
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);
tabHost.setCurrentTab(0); // default tab
}
}
What are the changes do I need to make in order for the Tabbar to got down? I have created other classes like tab1.java,tab2.java etc..
I have seen many people telling that you can set setContentView outside the oncreate method, but I didn't find anywhere an example. Now when I try to use setContentView, my app just crashes. Here is my source code:
AlarmActivity.java:
package com.alarm.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button buton = new Button(this);
buton.setId(101);
buton.setText("New alarm");
buton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(buton);
setContentView(layout1);
Button nou =(Button)findViewById(101);
nou.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
New_ala nou1=new New_ala();
nou1.nou_alarma();
}
});
}
}
New_ala.java:
package com.alarm.example;
public class New_ala extends AlarmActivity{
public void nou_alarma() {
setContentView(R.layout.timepicker);
}
}
TimePicker.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TimePicker
android:id="#+id/timePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/picker_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save">
</Button>
<Button
android:id="#+id/picker_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
On more detail, I can use setContentView(R.layout.timepicker) inside the oncreate method without any problems so the problem is that setContentView isn't working properly inside the New_ala.java class. Can anyone help me?
The code after setting the intent:
AlarmActivity:
package com.alarm.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, NewAlarmActivity.class));
}
}
NewAlarmActivity:
package com.alarm.example;
import android.os.Bundle;
public class NewAlarmActivity extends AlarmActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timepicker);
}
}
You can call setContentView any time you are running on the event (UI) thread. Be aware that when you do, any fields you initialized by calling findViewById will need to be reset.
The best way to do that is to have multiple activities: instead of nou1.nou_alarma();, create an intent and start a new activity with the new layout.
startActivity(new Intent(this, NewAlarmActivity.class));
And in the onCreate method of NewAlarmActivity, set the content view to R.layout.timepicker
The setContentView() method can be called only once per activity. If you want to completely change the layout at some point you should either go for ViewFlipper or have 2 layouts in the activity and show only one of them at given time by calling view.setVisibility(View.GONE); and view.setVisibility(View.VISIBLE); respectively.