How to work with tabhost,tabwidget and framelayout? - android-tabhost

I am using this code
package com.example.tabs;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Sími");
spec1.setContent(R.id.Sími);
spec1.setIndicator("Sími");
TabSpec spec2=tabHost.newTabSpec("Sms");
spec2.setIndicator("Sms");
spec2.setContent(R.id.Sms);
TabSpec spec3=tabHost.newTabSpec("Net");
spec3.setIndicator("Net");
spec3.setContent(R.id.Net);
TabSpec spec4=tabHost.newTabSpec("Greina");
spec4.setIndicator("Greina");
spec4.setContent(R.id.Greina);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.addTab(spec4);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
with this xml file
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/Sími"
android:layout_width="fill_parent"
android:layout_height="340dp"
android:orientation="vertical"
android:paddingTop="60px" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Sms"
android:orientation="vertical"
android:paddingTop="60px"
>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Net"
android:orientation="vertical"
android:paddingTop="60px"
>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Greina"
android:orientation="vertical"
android:paddingTop="60px"
>
</LinearLayout>
</FrameLayout>
</TabHost>
I am new to android eclipse. I made 4 tabs(linearlayout) inside this tabview. But it is difficult to set the location on a button in one tab. How can i work with this 4 types of tabs like i am working with 4 xml files, is that possible ? How can i make the tabwidget smaller without the text not going away? , i hope this makes sense.

Related

error while spec.setContent(Intent) on tabSpace

There is an error while i am putting Intent as spec.setContent(Intent); on tabSpace,and it works fine if i put a layout on spec.setContent(tab1);
And following is the error:-
did you forget to call 'public void setup(localactivitymanager
activitygroup)
Here is the class:-
public class test extends AppCompatActivity {
Point size;
RelativeLayout r1;
CallbackManager callbackManager;
Context context;
TabHost.TabSpec spec_signin,spec_signup;
#Override
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);
context = getApplicationContext();
Display display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(),R.drawable.accont_bannre),size.x,size.y,true);
Drawable dr = new BitmapDrawable(bmp);
r1 = (RelativeLayout)findViewById(R.id.r1);
r1.setBackgroundDrawable(dr);
TabHost host = (TabHost)this.findViewById(R.id.tabhost);
host.setup();
//Tab 1
spec_signin = host.newTabSpec("SIGNIN");
Intent songsIntent = new Intent(this, NotificationActivity.class);
spec_signin.setContent(songsIntent);
spec_signin.setIndicator("SIGN IN", getResources().getDrawable(R.drawable.active_arrow));
// spec_signin.setIndicator("SIGNIN");
host.addTab(spec_signin);
//Tab 2
spec_signup = host.newTabSpec("SIGNUP");
Intent Intent = new Intent(this, NotificationActivity.class);
spec_signup.setContent(Intent);
spec_signup.setIndicator("SIGN UP", getResources().getDrawable(R.drawable.active_arrow));
// spec_signup.setIndicator("SIGNUP");
host.addTab(spec_signup);
}//oncreate ends
}
and the XML:-<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabHost
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/r2">
<RelativeLayout
android:id="#+id/r1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="270dp">
<LinearLayout
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_above="#+id/tabs"
android:id="#+id/v1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ccc7c7"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<FrameLayout
android:background="#dede"
android:layout_below="#+id/r1"
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
</TabHost>
</RelativeLayout>
`
i have added following lines and it running.
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
host.setup(mLocalActivityManager);

TransitionManager scene animation behavior with root layout

I'm testing out the Android Kit Kat transition feature. When the transition happens, the margins of the root layout are getting padded. I find this behavior strange.
The source
public class MainActivity extends ActionBarActivity {
Button btn;
TextView txt;
#TargetApi(Build.VERSION_CODES.KITKAT)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.text);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewGroup root = (ViewGroup)findViewById(R.id.root);
Scene scene = Scene.getSceneForLayout(root, R.layout.activity_main2, MainActivity.this);
TransitionManager.go(scene);
}
});
}
...
}
activity_main.xml
<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"
android:id="#+id/root"
tools:context="com.sebouh00.test.scenes.MainActivity">
<Button android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:layout_marginBottom="12dp"/>
<TextView
android:id="#+id/text"
android:layout_below="#id/btn"
android:text="#string/hello_world"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18dp"/>
</RelativeLayout>
activity_main2.xml
<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"
android:id="#+id/root"
tools:context="com.sebouh00.test.scenes.MainActivity">
<Button android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:layout_marginBottom="12dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btn"
android:id="#+id/img"
android:src="#drawable/flag"
android:layout_marginBottom="12dp"/>
<TextView
android:id="#+id/text"
android:layout_below="#id/img"
android:text="#string/hello_world"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18dp"/>
</RelativeLayout>
Results in this unless I remove the paddings from activity_main2.xml
Any ideas why?
It's because the View produced by inflating activity_main2 gets inserted into the Scene's root, i.e. your R.id.root RelativeLayout, so the padding from R.id.root is still applied.

Custom Buttons - Error in Java - widget.Button;

Followed example to the letter. When I try to use a custom button via a custom.xml, emulator crashes with a Java error - inflating class android.widget.Button.
import android.os.Bundle;
import android.content.Intent;
import android.app.Activity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MasterActivity extends Activity {
Button uccBtn, tipBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
uccBtn = (Button) findViewById(R.id.button1);
tipBtn = (Button) findViewById(R.id.button2);
uccBtn.setOnClickListener (uccApp);
tipBtn.setOnClickListener (tipApp);
custom.xml
<?xml version="1.0" encoding="UTF-8"?>
<Selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:state_pressed="true" android:drawable="#drawable/btn_ecolab_selected"></item>
<item android:state_focused="true"
android:drawable="#drawable/btn_ecolab_highlight"></item>
<item android:drawable="#drawable/btn_ecolab"></item>
</Selector>
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background ="#ffffcc"
tools:context=".MasterActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="#string/button1" android:typeface="serif"
android:background="#drawable/custom"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/version"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="56dp"
android:text="#string/button10" android:typeface="serif"
android:background="#drawable/custom" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button10"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="#string/button2" android:typeface="serif"
android:background="#drawable/custom"
/>
</RelativeLayout>
If I reference just the button by name (btn_ecolab), it works fine, just no animation of the buttons when pressed or selected. I've tried it with a LinearLayout to make sure that the Relitavelayout was not the problem. The custom buttons are in my Res/drawable-hpdi folder. Also tried copies of buttons and custom.xml in the -mdpi, -xhdpi and -xxhdpi. Tried without #string assignments.
try this one.
uccBtn = (Button) findViewById(R.id.button1);
tipBtn = (Button) findViewById(R.id.button2);
uccBtn.setOnClickListener (new OnClickListener() {
#Override
public void onClick(View v) {
//your code is here
}
});
make one folder drawable in res folder put your custom.xml
I am using this way and work great.

Get value from custom dialog edittext in android

I my project i have a text view and button on my screen. On button click event i show custom dialog box (Dialog box content Edit text and a button). I put some data in dialog box edit text and click dialog box save button.
Now i want to show value of dialog edit text on screen text view on button click. How can i do this....
holder.list1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final TextView tv=(TextView)v.findViewById(R.id.textView3);
final Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.customdailogfroaddmilk);
dialog.setTitle("Account");
// set the custom dialog components - text, image and button
final EditText text = (EditText) dialog.findViewById(R.id.editText1);
text.setText(tv.getText().toString());
Button save = (Button) dialog.findViewById(R.id.button1);
Button cancle = (Button) dialog.findViewById(R.id.button2);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
tv.setText(text.getText().toString());
}
});
cancle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
on button(save) click event edittext(save) value not set in textview(tv)
?????.....
Create a layout in your layout folder and provide it as the View to a Dialog Object.
And for the example shown below, you need to have a TextView, a Button and EditText in that layout.
final Dialog dialog = new Dialog(context,
android.R.style.Theme_Translucent_NoTitleBar);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.xmlfile);
Button ok = (Button) dialog.findViewById(R.id.alert_ok_button);
TextView alert_title = (TextView ) dialog.findViewById(R.id.alert_title);
final EditText shelf_name_edit=(EditText)dialog.findViewById(R.id.shelf_name_edit_area);
alert_title.setText(title);
alert_title.setTextSize(20);
ok.setText("OK");
ok.setTextSize(20);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.cancel();
Log.i("EditText Value",shelf_name_edit.getEditableText().toString());
}
});
dialog.show();
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/ClkBtn"
android:text="Click Me"></Button>
</RelativeLayout>
mydialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff" android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView android:id="#+id/Tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px"
android:textColor="#6aa4cc"
android:text="Friendcaster for Facebook"
android:padding="10dip"
/>
<TextView android:id="#+id/Tv2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="14px"
android:textColor="#android:color/black"
android:layout_below="#+id/Tv1"
android:textStyle="bold"
android:text="A , B , C , D and or 5 friend like your status "
android:padding="10dip"
/>
<View android:layout_height="15dip" android:layout_width="fill_parent"/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent"
android:orientation="horizontal" android:background="#d6d6d6"
android:layout_weight="3"
>
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="left"
android:id="#+id/ll"
android:layout_weight="1" android:padding="5dip"
>
<ImageView android:src="#drawable/video"
android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="view" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#0388e5"
/>
</LinearLayout>
<View android:layout_height="match_parent" android:layout_width="1dip" android:background="#android:color/darker_gray"/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1" android:padding="5dip"
>
<ImageView android:src="#drawable/chat"
android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Reply" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#0388e5"
/>
</LinearLayout>
<View android:layout_height="match_parent" android:layout_width="1dip" android:background="#android:color/darker_gray"/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_weight="1" android:padding="5dip"
android:gravity="right"
>
<ImageView android:src="#drawable/cancel"
android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Zip" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#0388e5"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
main.java
package com.example.cusmizepopup;
import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
Dialog myDialog;
Button myButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.ClkBtn);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
myDialog = new Dialog(MainActivity.this);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
myDialog.setContentView(R.layout.mydialog);
//myDialog.setTitle("My Dialog");
myDialog.setCancelable(true);
//myDialog.setCancelable(true);
//myDialog.setCancelMessage("Are you Sure ? ")
LinearLayout button = (LinearLayout) myDialog.findViewById(R.id.ll);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
//myDialog.dismiss();
Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show();
}
});
myDialog.unregisterForContextMenu(myButton);
myDialog.show();
}
});
}
}

Tab host autoscroll – Programmatically

I want to develop an application simulating the tab browsing like Dolphin HD. Below is the XML file I have used to create the UI.
My queries are,
I am not able to get the fixed size tab like dolphin HD.
When I have created around 4-5 tabs, now I want to programmatically set the focus to new tab and scroll it to the current view.
Similar query was posted in the below link, but its not working still…
Android - Programmatic scrolling of TabWidget
In add new tab method, I tried inserting the below lines as mentioned in the above link…. but it didn’t work…
tabHost.setFocusableInTouchMode(true);
tabHost.setCurrentTab(z);
tabHost.setFocusable(true);
//Vinod
for ( i = 0; i < z; i++) {
getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
XML file:-
<?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="wrap_content"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<HorizontalScrollView android:layout_width="420px"
android:id="#+id/tab1"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="10px"
android:layout_height="wrap_content"
android:tag="tabPane">
</TabWidget>
</HorizontalScrollView>
<ImageButton
android:background="#android:color/transparent"
android:layout_marginTop="10px"
android:id="#+id/add_btn"
android:layout_height="70px"
android:layout_width="70px"
android:src="#android:drawable/ic_menu_add"
android:layout_toRightOf="#id/tab1"/>
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/address_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10px"
android:layout_width="fill_parent"
android:layout_height="70px"/>
</RelativeLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp" />
</LinearLayout>
</TabHost>
The delay was due to timing, so just a delay would work perfectly alright.
HorizontalScrollView hv = (HorizontalScrollView)findViewById(id.tab1);
//hv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
for ( i = 0; i < z; i++) {
getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
hv.postDelayed(new Runnable() {
public void run()
{
HorizontalScrollView hv = (HorizontalScrollView)findViewById(id.tab1);
hv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}, 100L);