First post. I'm following a few courses on Android programming so fairly new but not a complete newbie. I have a background in programming from long ago..
For some odd reason, older projects have the OnClick running fine but every project I created today, with either Genymotion or AVD the OnClick never fires even in the following barebone example I created.
https://www.dropbox.com/s/js1qh263vldte0z/deleteme.zip?dl=0
Here's the original project I'm working on which I can't even click on the buttons (as if there's a transparency in front or something).
If someone can explain why when I open older projects based on 22 everything works but running now on 23 (with 16 as backward) OnClick doesn't work that'd be very nice of you. Thanks!
The Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="ca.shaarable.planetxerox.admin_add_products_hardware">
<LinearLayout
android:orientation="vertical"
android:id="#+id/admin_add_printer_first"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2">
<TextView
android:id="#+id/admin_add_printer_tv_name"
android:text="#string/admin_add_printer_tv_name"
android:textSize="12sp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/admin_add_printer_et_name"
android:text="#string/admin_add_printer_et_name"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="#+id/admin_add_printer_second"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.65">
<TextView
android:id="#+id/admin_add_printer_tv_thumbnail"
android:text="#string/admin_add_printer_tv_thumbnail"
android:textStyle="italic"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/admin_add_printer_image"
android:src="#drawable/igen5"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="#+id/admin_add_printer_third"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15">
<LinearLayout
android:id="#+id/admin_add_printer_sublayout"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="1"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/admin_add_printer_CANCEL"
android:text="#string/admin_add_printer_CANCEL"
android:background="#F99F1C"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/admin_add_printer_ADD"
android:text="#string/admin_add_printer_ADD"
android:background="#2ABDBA"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And here's the code for the class:
package ca.shaarableapps.presssupport;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class AdminAddPrinter extends AppCompatActivity implements View.OnClickListener
{
private EditText printerName;
private ImageView printerThumbnail;
private Button cancelBtn, addBtn;
private static final String TAG = "MyActivity";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_add_printer);
printerName = (EditText)findViewById(R.id.admin_add_printer_et_name);
printerThumbnail = (ImageView)findViewById(R.id.admin_add_printer_image);
cancelBtn = (Button)findViewById(R.id.admin_add_printer_CANCEL);
addBtn = (Button)findViewById(R.id.admin_add_printer_ADD);
}
#Override
public void onClick(View v)
{
Log.v(TAG, "Nooooooooooooo");
switch (v.getId())
{
case R.id.admin_add_printer_ADD:
addBtn.setText("Good Job");
break;
}
}
}
This is the layout as seen by the user...
Try to add .setOnClickListener(this); inside onCreate(...) method.
protected void onCreate(Bundle savedInstanceState){
...
addBtn.setOnClickListener(this);
}
Hope this help
The problem is here in you code.
<Button
android:id="#+id/admin_add_printer_ADD"
android:text="#string/admin_add_printer_ADD"
android:background="#2ABDBA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick" // add this line
/>
Related
I'm having trouble launching the app (the app freezes) after adding a Spinner to it.
It probably doesn't get me "Context" properly - probably.
Maybe the problem lies elsewhere, but I cannot verify it at my stage
Before adding Spinner everything worked ok.
Git app code: https://github.com/RemekLago/project_Factory2.git
I will be grateful for any help.
code of main activity:
package com.example.projecttech_v4;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager2 viewPager2;
private PagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tabLayout);
viewPager2 = findViewById(R.id.viewPager2);
tabLayout.addTab(tabLayout.newTab().setText("Maszyna01"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna02"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna03"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna04"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna05"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna06"));
FragmentManager fragmentManager = getSupportFragmentManager();
adapter = new PagerAdapter(fragmentManager, getLifecycle());
viewPager2.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position){
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
Spinner spinner1 = findViewById(R.id.spinner1_operator);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource
(this, R.array.operator, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
}
}
code: XML mainactivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TableRow
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner1_operator"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner2_data"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner3_godzina"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/button_zatwierdz_operator"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Zatwierdź"
android:textSize="11sp"
android:textColor="#color/black"
android:layout_weight="1"/>
</TableRow>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabGravity="fill"
app:tabIndicatorAnimationMode="elastic"
app:tabIndicatorGravity="stretch"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/white" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
TableRow should be a child of TableLayout only. exchange <TableRow to <LinearLayout with android:orientation="horizontal" attribute in your XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- TableRow becomes LinearLayout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp">
<!-- spinners and rest of code -->
powodzenia
As in the topic, I want to start the method when I press the button on the headphones. There are a lot of such topics, but none does raise the problem when the method is in the onClickListener. I have tried many solutions, but I can not find the right one.
My xml code:
<?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: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"
android:orientation="vertical"
tools:context="com.example.bartek.przyciskidosluchawek.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20">
<Button
android:text="#string/but1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/but_niebieski"
android:layout_weight="3"
android:contentDescription="#string/but1"
android:onClick="klik_niebieski" />
<Button
android:text="#string/but2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/but_czerwony"
android:layout_weight="3"
android:onClick="klik_czerwony" />
<Button
android:text="#string/but3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/but_zolty"
android:layout_weight="3"
android:onClick="klik_zolty" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5">
<Button
android:text="#string/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btn_zmiana" />
</LinearLayout>
</LinearLayout>
My MainActivity:
package com.example.bartek.przyciskidosluchawek;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button but_nieb;
private Button but_czerw;
private Button but_zolt;
private Button but_zmiana;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but_nieb = (Button)findViewById(R.id.but_niebieski);
but_czerw = (Button)findViewById(R.id.but_czerwony);
but_zolt = (Button)findViewById(R.id.but_zolty);
but_zmiana = (Button)findViewById(R.id.btn_zmiana);
but_nieb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
but_zmiana.setBackgroundColor(Color.BLUE);
}
});
but_czerw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
but_zmiana.setBackgroundColor(Color.RED);
}
});
but_zolt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
but_zmiana.setBackgroundColor(Color.YELLOW);
}
});
}
}
This is a simple application, because I care about the method in the onClickListener and use the buttons on the headphones, and she perfectly illustrates my problem.
Does anyone know how to do this?
Thank you in advance.
You need to add a BroadcastReceiver with the Intent filter
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
and then process those Intents to get the actual button press. Alternatively you could take a look at https://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver.html for a more complete guide.
For my App I need to create a layout similar to this:
I have been thinking about having a RecyclerView inside a CardView. But if I do it like that I have to create two separate row.xml (one for first and second row and the other for third and fourth). I was wondering if there is a simpler way to get the same result.
Can you point in the right direction?
Create it using Relative layout or linear layout,and set id for parent layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/card_view_notification"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="7dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="#+id/rel_one"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Version"
android:id="#+id/hedone"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5.3(123)"
android:textColor="#000000"
android:layout_below="#+id/hedone"
android:textSize="14sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#7B7A7F"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="#+id/relative_two"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Version"
android:id="#+id/hedtwo"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Disponibile"
android:textColor="#000000"
android:layout_below="#+id/hedtwo"
android:textSize="14sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#7B7A7F"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="#+id/relative_three"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Your text"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#7B7A7F"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="#+id/relative_four"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Yourtext"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
OUTPUT
Take a listview and initially make bottom textview gone. Make it visible as and when needed. Using cardview wont help you create such layout.
Maybe you could create a single row.xml and hide the subtitle when it's necessary.
mySubtitleTextView.setVisibility(View.GONE) like that subtitle doesn't take any space and title is still in center.
I would suggest making multiple card views and using the getItemViewType method. You can set various view types for different layouts and in the onCreateViewHolder method you can inflate the card view based on the view type.
How to create RecyclerView with multiple view type?
First of all a point with in a RecyclerView there will be CardView not reverse .if you want to do it with RecyclerView then follow some steps like:
step 1:
create a Layout named as Activity_main.xml which having a RecyclerView like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
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=".ui.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_view"
android:scrollbars="vertical"
></android.support.v7.widget.RecyclerView>
</RelativeLayout>
step 2. Create layout named as product_layout which having a CardView like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp">
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Products"-->
<!--android:gravity="center"/>-->
<!--<RelativeLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content">-->
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:gravity="center"-->
<!--android:textStyle="italic"-->
<!--android:text="Products"-->
<!--android:textAppearance="?android:textAppearanceLarge"/>-->
<!--</RelativeLayout>-->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/smartphone"
android:id="#+id/mobileimage"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SmartPhones"
android:layout_toRightOf="#+id/mobileimage"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/productname"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
step 3. Now set first RecyclerView layout in MainActivity.java, if you are working on json then parse it after that set Your adapter like:
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
adapter = new ProductAdapter(list, getApplicationContext());
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
step 4. create an Adapter named as ProductAdapter.java dafile like:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.dharmendras.productdetail.models.Products;
import com.example.dharmendras.productdetail.R;
import java.util.ArrayList;
public class ProductAdapter extends RecyclerView.Adapter <ProductAdapter.ProductsViewHolder> {
ArrayList<Products> products = new ArrayList<Products>();
private Context context;
public ProductAdapter(ArrayList<Products> products,Context context){
this.context = context;
this.products = products;
}
#Override
public ProductsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_layout,parent,false);
ProductsViewHolder productsViewHolder = new ProductsViewHolder(view);
return productsViewHolder;
}
#Override
public void onBindViewHolder(ProductsViewHolder holder, int position) {
Products pdt = products.get(position);
//Picasso.with(context).load(pdt.getImage_url()).resize(120, 60).into(holder.product_img);
//pdt.getProduct_name()
holder.product_name.setText(pdt.getProduct_name());
//Drawable myDrawable = context.getResources().getDrawable(pdt.getImage_url());
// int id = getContext().getResources().getIdentifier("imageName", "drawable", getContext().getPackageName());
// ImageView myImageView = (ImageView)findViewById(R.id.myImage);
//myImageView.setImageResource(R.drawable.icon);
// holder.product_img.setImageDrawable(pdt.getImage_url());
//imageView.setBackground(getResources().getDrawable(getResources().getIdentifier("name","id",getPackageName())));
String name = pdt.getImage_url();
int id = context.getResources().getIdentifier(name, "drawable",context.getPackageName());
Drawable drawable = context.getResources().getDrawable(id);
holder.product_img.setImageDrawable(drawable);
}
#Override
public int getItemCount() {
return products.size();
}
public static class ProductsViewHolder extends RecyclerView.ViewHolder{
ImageView product_img;
TextView product_name;
public ProductsViewHolder(View view){
super(view);
product_img = (ImageView) view.findViewById(R.id.mobileimage);
product_name = (TextView) view.findViewById(R.id.productname);
}
}
}
Step 5. if u want to add RecyclerView clickable then set onClickListener on RecyclerView in your Activity_main.java.
i want a page with text on top(EditText) then image (in FragmentLayout) and then two tabs below that image.
my output is not showing any tab in output but i have written code to implement tabs.
this code is notshowing any error and in output i mam not getting any tab in output
i am not getting correct output and this is not even showing any error
i tried it without extending TabActivity also (only with extens Activity) but still output is same whose image is loaded above
Code of activity_main.xml is
<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=".MainActivity" >
<TextView
android:id="#+id/tbol"
android:layout_width="250dp"
android:layout_height="30dp"
android:text="#string/heading"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#F00"
android:textSize="25sp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#drawable/leaf" >
</FrameLayout>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
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" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
code of MainActivity.java is
package com.example.tabhost;
import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
#SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
TabHost th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
th=getTabHost();
//For desc tab
TabSpec descspec=th.newTabSpec("Description");
descspec.setIndicator("Description", null);
Intent firstintent=new Intent(this, DecsriptionTab.class);
descspec.setContent(firstintent);
//For Information Tab
TabSpec infospec=th.newTabSpec("Information");
infospec.setIndicator("Information", null);
Intent secondintent=new Intent(this, Info_Tab.class);
descspec.setContent(secondintent);
}
}
this is code of 2 .xml files
desc_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="This is desc tab"
android:padding="15dp"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
info_layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="This is info tab"
android:padding="15dp"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This is code of corresponding java files
DecsriptionTab.java
package com.example.tabhost;
import android.app.Activity;
import android.os.Bundle;
public class DecsriptionTab extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.desc_layout);
}
}
Info_Tab.java
package com.example.tabhost;
import android.app.Activity;
import android.os.Bundle;
public class Info_Tab extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info_layout);
}
}
The activity_main.xml is like this
<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" >
<Button
android:id="#+id/button_one_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Button"
/>
<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_fragment" />
<Button
android:id="#+id/button_two_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Button"
/>
</LinearLayout>
The main activity class is like this
package com.example.testfragmentshoneycomb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
first_fragment.xml is like this
<?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="wrap_content"
android:orientation="vertical"
android:background="#color/grey" >"
<TextView
android:id="#+id/text_view_one_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View one" />
<TextView
android:id="#+id/text_view_two_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View two" />
<TextView
android:id="#+id/text_view_three_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View three" />
</LinearLayout>
FirstFragment class is like this
package fragments;
import com.example.testfragmentshoneycomb.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, container, false);
return view;
}
}
It shows only first button and nothing else on the screen. If I remove the first button from activity_main.xml it shows the fragment but doesn't show the second button.
Min SDK version is 11 and build target is android 4.1
set android:orientation="vertical" in your activity layout.
Its because by default LinearLayout's orientation is horizontal. Therefore the whole screens width is captured by the First Button and Fragment.
Are you sure you want to see it like?
First_Button Fragment Second_Button
If Yes the use layout_weight. If No then give orientation=vertical to LinearLayout which will show your layout output as
First_Button
Fragment
Second_Button
set LinearLayout orientation to vertical, it's horizontal by default.
read docs carefully
Use the following layout :
<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" >
<Button
android:id="#+id/button_one_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Button"
/>
<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_fragment" />
<Button
android:id="#+id/button_two_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Button"
/>
</LinearLayout>