Problem using TabActivity - Java for Android App Development

Im starting to create my app, well the layout of the app. My app will consist of Tabs on the top part of the screen. Question is, how do I do that? I know its simple but yet I cant find the answer. Ive googled it, tried various methods, still errors. The TabActivity has been deprecated so I dragged the tab activity onto my layout using the palette, changed the tabs orientation to veritcal and have no idea what do from there. Ive tried the tutoirals on the developers pages by google but still no luck..

It is recommended to add them to the ActionBar.
Without your code we cannot help you. How often do we have to tell you that?

nikwen said:
It is recommended to add them to the ActionBar.
Without your code we cannot help you. How often do we have to tell you that?
Click to expand...
Click to collapse
Sorry keep on forgetting to include it.
XML:
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<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" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
Theres nothing really on the class. just the default methods:
Code:
package com.examples.hello;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I haven't included other classes as I have tried to link them but have no idea how to so I deleted them. My minimum API level is on 8.

No problem.
However, adding tabs to the Activity is a little bit more complicated.
The way they want to do it is by using the ActionBar. There is a cool library which offers the ActionBar for old versions of Android: ActionBarSherlock.
I always do it that way: http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-fragment-tabs-in-android/

Related

[Q] ArrayAdapter and custom layout

Hi , i'm studyng android Adapter
I want create a ListView with a serie of data. Ok , the code.
Code:
/*
Activity class
*/
package it.actiondesign.android.app.userdata;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class UserDataActivity extends Activity {
ArrayList<UserData> allUsers = new ArrayList<UserData>();
private ListView myListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListView= (ListView) findViewById(R.id.datalist);
createFakeData();
int resID = R.layout.user_row;
// data_adpater = new ArrayAdapter<UserData>(this, R.layout.user_row, allUsers);
// myListView.setAdapter(data_adpater);
}
//fill ArrayList with fake data
private void createFakeData() {
UserData first = new UserData("Frank", "Smith", 30, "Chicaco");
UserData second = new UserData("Tania", "Roger", 20, "Los Angeles");
allUsers.add(first);
allUsers.add(second);
}
}
here the main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/datalist"></ListView>
</LinearLayout>
I create a class of data
Code:
public class UserData {
private String name;
private String surname;
private int age;
private String from;
public UserData(String _name , String _surname, int _age, String _from){
this.name= _name;
this.surname= _surname;
this.age= _age;
this.from= _from;
}
public String getName(){
return name;
}
public String getSurname(){
return surname;
}
public int getAge(){
return age;
}
public String getFrom(){
return from;
}
}
custom layout of a single item
Code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Name: "
android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text=""
android:id="@+id/tName" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/red">
</TextView>
</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Surname: "
android:id="@+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text=""
android:id="@+id/tSurname" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/red">
</TextView>
</TableRow>
<TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="From: "
android:id="@+id/TextView03" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text=""
android:id="@+id/tFrom" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/red">
</TextView>
</TableRow>
<TableRow android:id="@+id/TableRow04" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Age: "
android:id="@+id/TextView03" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text=""
android:id="@+id/tAge" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/red">
</TextView>
</TableRow>
</TableLayout>
In other layout a create a ArrayAdpater<String> and all was simple, but how can fill TextView (tName, tSurname, tAge, tFrom) with data.
Now I use a simple ArrayList after i load data from WebService
Depends what kind of data your are retrieving. In your layout you have 4 textview 'rows'. Maybe instead of this you could use a listview?

[Q] not reaching onCreateOptionsMenu

I am trying to create an action bar.
I have an application with DrawerLayout that has inside a ViewPager... and I implement a PagerAadapter and a class for the page fragment.
this is the activity_main.xml under layout folder
Code:
<android.support.v4.widget.DrawerLayout
xmlns:android=<REMOVED_LINK>
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- This is the main-->
<android.support.v4.view.ViewPager xmlns:android=<REMOVED_LINK>
xmlns:tools=<REMOVED_LINK>
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
<ListView
android:id="@+id/optionsMenu"
android:background="#000000"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
And this is the main.xml found in menu folder
Code:
<menu xmlns:android=<REMOVED_LINK>>
<item android:id="@+id/action_settings"
android:title="@string/action_settings" />
<item android:id="@+id/action_details"
android:title="@string/action_details" />
<item android:id="@+id/action_details2"
android:title="@string/action_details2" />
</menu>
The problem is that It doesn't even reach onCreateOptionsMenu on the MainActivity... I tried everything: - change android:minSdkVersion to 7, 11 and 14 (in the manifest uses-sdk tag) - add attribute to menu items "showAsAction", and remove that attribute too - make my page fragment override onCreateOptionsMenu
i debug it and it never reaches onCreateOptionsMenu, anywhere. and the getActionBar() is of course null.
Do you want it to be the native ActionBar, the ActionBarSherlock one or the ActionBarCompat one?
nikwen said:
Do you want it to be the native ActionBar, the ActionBarSherlock one or the ActionBarCompat one?
Click to expand...
Click to collapse
native ActionBar...
but as soon as I manage to reach onCreateOptionsMenu then I can do everything.
but i'm stuck on this wierd problem... maybe I should make a new project

[Q]Dealing with fragments

Here is the xml
Code:
<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"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40.0dip"
android:background="#ffffffff"
android:id="@+id/progaction"
android:layout_weight="0.2"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_below="@id/container"
android:layout_alignParentBottom="true">
<TextView
android:gravity="center"
android:text="Level"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<SeekBar
android:gravity="center"
android:layout_weight="1"
android:id="@+id/seek"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/><TextView
android:gravity="center"
android:text="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/prog"
/>
</LinearLayout>
</LinearLayout>
Framelayout with id=container contains the fragment used in the activity.
Now the problem is that how to can i get the value of the textView(id=prog) in a particular fragment.i am not able to do it
Error says that "this method cant be used for static context" for the following line
Code:
TextView prog=(TextView) findViewById(R.id.prog);
So please help me to get the value of textView in a fragment
Sent from my GT-S5570 using XDA Premium 4 mobile app
I think your need to call getActivity() first and after this perform activity.findViewById(R.id.prog):
HTML:
Activity a = getActivity();
TextView prog=(TextView) a.findViewById(R.id.prog);
well maybe the easiest way is to hold an instance of the textView in the fragment code, then have a public method to return that instance, so that in the main fragment activity you simply get an instance to the fragment then call the method ?
Have you tried this one? http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment-android
danelab said:
I think your need to call getActivity() first and after this perform activity.findViewById(R.id.prog):
HTML:
Activity a = getActivity();
TextView prog=(TextView) a.findViewById(R.id.prog);
Click to expand...
Click to collapse
Thanks.it worked
nikwen said:
Have you tried this one? http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment-android
Click to expand...
Click to collapse
This is to be used if textView is present in the XML inflated by the fragment itself .but my textView is in XML of mainactivity.
Anyways thanks for the answer.I got the solution
Sent from my GT-S5570 using XDA Premium 4 mobile app

[Q] Padding for ExpandableListView-Group

Hello,
I have a ExpandableListView and I'm using a custom layout for the groups:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceListItem" >
<TextView
android:id="@+id/tvGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="0dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textIsSelectable="false" />
</LinearLayout>
The problem: The padding displays only right in Android 4.4, in older versions the padding is ignored..why?
The image I attached shows four different virtual devices running different Android versions.

Could someone help me with this flat button design?

. I copied
everything as he said, and I use it in xml and I can see in the preview that I have a specific design that looks close to his design
Code:
<RelativeLayout xmlns:android="htt/p://schemas.android.co/m/apk/res/android"
xmlns:fbutton="ht/tp://schemas.android.co/m/apk/res-auto"
xmlns:tools="ht/tp://schemas.android.c/om/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=".MainActivity"
>
<info.hoang8f.widget.FButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:layout_margin="10dp"
android:minHeight="@dimen/demo_button_min_height"
android:minWidth="@dimen/demo_button_min_width"
android:text="@string/buttonString"
android:textColor="@android:color/white"
fbutton:buttonColor="@color/fbutton_color_emerald"
fbutton:shadowEnabled="true" />
</RelativeLayout>
but I get this error
Code:
Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\userpc\AppData\Local\Android\sdk\build-tools\21.1.2\dx.bat --dex --no-optimize --output C:\Users\userpc\AndroidStudioProjects\Project\app\build\intermediates\dex\debug --input-list=C:\Users\userpc\AndroidStudioProjects\Project\app\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Linfo/hoang8f/widget/FButton;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
Edit: I found the problem, but I can't delete the post. I was having something extra in the gradle settings. Sorry for this post.
Please explain what extra thing you had so as to help others as well.

Categories

Resources