Android array result shows in multiple textview - android

I'm trying to show the result which I got from my array function which has multiple result into the textview
for example, if the result is 1, 3, 5, 7, 9, each result will be shown in each textview. There will be 10 textviews and only 5 textviews will be used in this case.
is anyone know how to do it?
//this function is used to determine how many page can be flipped. I used viewFlipper in this case. rangeValue is depending on the user input
public int binaryRangePage(int rangeValue){
int flipperPage = 1;
while(rangeValue >= 2){
rangeValue = rangeValue/2;
flipperPage++;
}
return flipperPage;
}
totalPage = binaryRangePage(rangeMode);
//initialization range for 2 dimensional table
binaryTable = new int[rangeMode+1][totalPage+1];
int n;
int i;
int k;
//looping the range
for(i=1; i<=rangeMode; i++){
n = i;
flipperPage = 1;
//looping for each page in viewFlipper
while(n>0){
remainder = n%2;
binaryTable[i][flipperPage] = remainder;
n = (int) Math.floor(n/2);
System.out.println("remainder["+i+"]["+flipperPage+"]: "+remainder);
flipperPage++;
}
//if the current page is less than total page which should has value, the remainder page will be filled with 0
if(flipperPage<=totalPage){
//looping untuk remainder page dgn 0
for(k=flipperPage; k<=totalPage;k++){
binaryTable[i][flipperPage] = 0;
System.out.println("remainder["+i+"]["+flipperPage+"]: 0");
flipperPage++;
}
}
I'm trying to shows the value of range which has remainder 1. for example, the range is 10, there will be 4 page which can be flipped. in the page 1, the value is 1, 3, 5, 7, and 9. these value will be showed in the textview

Sample code In xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
</LinearLayout>
And in java code :
private static String[] array = {"a", "b", "c"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.textview);
TextView text1 = (TextView) findViewById(R.id.textview1);
TextView text2 = (TextView) findViewById(R.id.textview2);
text.setText(array[0]);
text1.setText(array[1]);
text2.setText(array[2]);
}
It's just sample change your array and textviews.

You need to create your TextViews dynamically. This is the optimum way.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
for (int i = 0; i <= array.length ; i++){
TextView tv = new TextView(getApplicationContext());
tv.setText(array[i]);
layout.addView(tv);
}
But since you tell that your TextViews are fixed, you need to do something which is not really "appropriate" for programming but here you go:
if (array[0]!=null){
textView1.setText(array[0]);
}
else{
textView1.setVisibility(View.GONE);
}
if (array[1]!=null){
textView2.setText(array[1]);
}
else{
textView2.setVisibility(View.GONE);
}
.
.
.
if (array[9]!=null){
textView10.setText(array[9]);
}
else{
textView10.setVisibility(View.GONE);
}

Related

How to prepare a dial-like GridLayout?

Background
I'm trying to create a dial-like GridLayout, similar to this sketch :
The problem
I'm failing to create it correctly.
The idea I had was that all cells would take the same space, spreading the size evenly, except for the backspace button on the right that would span over 3 rows.
What I tried
What I currently have is this:
private TextView generateGridTextButton(final CharSequence textToShowAndAddUponClick) {
TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.grid_text_button, mButtonsGrid, false);
tv.setText(textToShowAndAddUponClick);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View view) {
//...
}
});
return tv;
}
private void initButtonsGrid() {
for (int i = 1; i <= 3; ++i)
mButtonsGrid.addView(generateGridTextButton(Integer.toString(i)));
final ImageView backspaceButton = new ImageView(this);
backspaceButton.setImageResource(android.R.drawable.sym_def_app_icon);
final LayoutParams backspaceButtonLayoutParams = new LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 3, 3), GridLayout.spec(GridLayout.UNDEFINED, 1, 1));
backspaceButtonLayoutParams.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
backspaceButtonLayoutParams.height = backspaceButtonLayoutParams.width = LayoutParams.WRAP_CONTENT;
backspaceButton.setLayoutParams(backspaceButtonLayoutParams);
backspaceButton.setBackground(...));
mButtonsGrid.addView(backspaceButton);
for (int i = 4; i <= 9; ++i)
mButtonsGrid.addView(generateGridTextButton(Integer.toString(i)));
backspaceButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View view) {
//...
}
});
mButtonsGrid.addView(generateGridTextButton("*"));
mButtonsGrid.addView(generateGridTextButton("0"));
mButtonsGrid.addView(generateGridTextButton("+"));
final ImageView actionButton = new ImageView(this);
actionButton.setImageResource(android.R.drawable.sym_def_app_icon);
final LayoutParams actionButtonLayoutParams = new LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1, 1), GridLayout.spec(GridLayout.UNDEFINED, 1, 1));
actionButtonLayoutParams.setGravity(Gravity.CENTER);
actionButtonLayoutParams.height = actionButtonLayoutParams.width = LayoutParams.WRAP_CONTENT;
actionButton.setLayoutParams(actionButtonLayoutParams);
actionButton.setBackground(...);
actionButton.setClickable(true);
mButtonsGrid.addView(actionButton);
}
res/layout/grid_text_button.xml
<TextView
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="wrap_content"
android:layout_height="wrap_content" android:layout_rowWeight="1"
android:background="#drawable/..." android:clickable="true" android:fontFamily="..."
android:gravity="center" android:textColor="#2a373e" android:textSize="36sp" app:layout_columnWeight="1"
app:layout_gravity="fill" tools:text="1"/>
The layout file has this:
<android.support.v7.widget.GridLayout android:id="#+id/gridLayout"
... app:columnCount="4"
app:orientation="horizontal" app:rowCount="4">
This almost works but for some reason the 2 top rows of buttons take less height than the 2 bottom rows of buttons:
I tried to play with the values of the gravity, the weight, the span... but nothing I tried helped (even got worse).
The question
What is wrong here? Why do the rows take different heights?
How can I fix this ?
The reason for this was that I used android:layout_rowWeight instead of app:layout_rowWeight , so it didn't work well on old Android versions.
Now it works fine:

Listview - Getview called multiple times

I have a ListView and a custom adapter. Now i have red posts about getView() method being called multiple times, and most of them have to do with the wrap_content feature of the ListView.
I have changed my height, so that it can be match_parent but still, the method is being called.
I think it has something to do with adding TextViews dynamically in the adapter, and i don't know how to do it, so it works properly.
If there is an alternative, i am opened to it, the only reason why i put TextViews is so that i can have letters written in different colors.
Here is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
Row current = mList.get(position);
/* if the given channel row view is not being updated*/
if (v == null)
{
/* inflate layout */
LayoutInflater vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item2, null,false);
}
v = setLinearLayout(v,current);
return v;
}
Here is the setLinearLayout() method:
private View setLinearLayout(View v,Row current) {
ArrayList<Integer> winResults = current.checkNumbers(mResult,mType);
int numbers[] = current.getNumbers();
int N = Global.getNumberOfNumbers(mType);
boolean FLAG_SET = false;
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
Log.d("PAVLE",""+i+" row is: "+current.getStringNumbers());
// create a new textview
final TextView rowTextView = new TextView(v.getContext());
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,75);
rowTextView.setTextSize(24.0f);
rowTextView.setPadding(8, 8, 8, 8);
rowTextView.setGravity(Gravity.CENTER);
rowTextView.setBackgroundColor(Color.BLACK);
rowTextView.setTypeface(null, Typeface.BOLD);
rowTextView.setLayoutParams(lp);
if(mType == R.string.sans_topu && i == 5 && !FLAG_SET){
i--;
rowTextView.setTextColor(Color.WHITE);
rowTextView.setText("+");
FLAG_SET = true;
}
else {
// set some properties of rowTextView or something
if (mWin == Global.WIN || mWin == Global.LOSE) {
if (winResults.contains(numbers[i]))
rowTextView.setTextColor(Color.GREEN);
else rowTextView.setTextColor(Color.RED);
} else {
rowTextView.setTextColor(Color.WHITE);
}
if (numbers[i] < 10) {
rowTextView.setText("0" + numbers[i]);
} else rowTextView.setText("" + numbers[i]);
}
// add the textview to the linearlayout
LinearLayout ll = (LinearLayout) v.findViewById(R.id.ll_item);
ll.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}
final TextView prize = new TextView(v.getContext());
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,75);
prize.setTextSize(24.0f);
prize.setPadding(8, 8, 8, 8);
prize.setGravity(Gravity.CENTER);
prize.setBackgroundColor(Color.BLACK);
prize.setTypeface(null, Typeface.BOLD);
prize.setTextColor(Color.WHITE);
prize.setLayoutParams(lp);
try {
String cash = findCorretAmmount(winResults, numbers);
prize.setText(cash);
mTotal.append(" "+cash);
}
catch (Exception e){
e.printStackTrace();
}
LinearLayout ll = (LinearLayout) v.findViewById(R.id.ll_item);
ll.addView(prize);
return v;
}
And a little bit of XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvRandomTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:paddingRight="8dp"
android:text="#string/your_ticket_numbers_"
android:textColor="#android:color/black"
android:textSize="28sp"
android:textStyle="bold"
/>
<View
android:id="#+id/divider4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/tvRandomTextView"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:background="#android:color/darker_gray"/>
<ListView
android:id="#+id/lvRowList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/divider4"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
>
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/delete"/>
<Button
android:id="#+id/btnShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/btn_share"/>
<Button
android:id="#+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/done"/>
</RelativeLayout>
<TextView
android:id="#+id/tvResultLink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:autoLink="web"
android:gravity="center"
android:linksClickable="true"
/>
Also worth mentioning is that list_item2(LinearLayout) has height of 100dp, it's fixed size.
I think its not about textview.
The getView is always called multiple times. If you have 10 items on the screen to be shown, the getView going to be called 15 times, because the android creating views that are not on the screen. It`s good, because when the user start scrolling, it is not going to lagging.
After the user left the item, the view get`s recycle and reused by the adapter. Lets say, you have a list with 10000000 item, but you have 5 item on the screen at all time. In this case - to save power, and improve performance - the android going to create 10 list item, and this 10 item is going to recylce and refresh by content.
ViewHolder pattern
Please read this and use this patter to improve your code performance:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Google about ListView:
http://developer.android.com/guide/topics/ui/layout/listview.html
Tutorials:
http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/

Get information checkbox and EditText within a LinearLayout

I have a checkbox and EditText that generated dynamically by a query
This is the method that generates the checkbox and EditText and adds it to LinearLayout
private void create_form() {
JSONObject json = null;
int count = 0;
lm = (LinearLayout) findViewById(R.id.linearMain);
int seccion = Integer.parseInt(conf.get_id_seccion());
int tipo_solicitud = Integer.parseInt(conf.get_tipo_solicitud());
JSONObject jobj = obj_sqlite.get_form(seccion, tipo_solicitud);
try {
count = Integer.parseInt(jobj.getString("cont"));
json = new JSONObject(jobj.getString("json"));
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
for (int x = 1; x <= count; x++) {
try {
JSONObject json_row = new JSONObject(json.getString("row" + x));
CheckBox chb = new CheckBox(this);
chb.setText(json_row.getString("pregunta"));
chb.setId(json_row.getInt("pregunta_verificacion_supervision"));
chb.setTextSize(10);
chb.setPadding(8, 3, 8, 3);
chb.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
chb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(chb);
EditText et = new EditText(this);
et.setHint("observaciones");
et.setId(json_row.getInt("pregunta_verificacion_supervision"));
et.setTextSize(10);
et.setPadding(8, 3, 8, 3);
et.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
et.setInputType(android.text.InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(et);
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
}
}
Now I need to get all this checkbox selected along with your EditText to keep them in the table
this is my 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.php_mysql_sqlite.Formulario_verificacion_supervision" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_activity_preguntas_revision" android:id="#+id/textView1"/>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="285dp"
android:layout_height="330dp"
android:layout_marginTop="30dp" >
<LinearLayout
android:id="#+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="#color/White"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/bt_regresar"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/scrollView1"
android:onClick="regresar"
android:text="#string/bt_regresar" />
<Button
android:id="#+id/bt_guardar"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/bt_finalizar"
android:layout_alignBottom="#+id/bt_finalizar"
android:layout_alignLeft="#+id/scrollView1"
android:text="#string/bt_guardar"
android:onClick="guardar" />
<Button
android:id="#+id/bt_finalizar"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/bt_regresar"
android:layout_alignBottom="#+id/bt_regresar"
android:layout_marginLeft="31dp"
android:layout_toRightOf="#+id/bt_guardar"
android:text="#string/bt_finalizar"
android:onClick="finalizar" />
and this is one image as is currently
http://i61.tinypic.com/2uo2hi8.jpg
by a method that is called on a button click, making the action to get the data
Thanks to all
PS: If you give me negative points, leave a comment of that because it has happened to me in the past and can not do wrong
You have two options.
Set a unique id to each checkbox and EditText and keep their ids in an ArrayList. When you want to do the check
for(int i = 0 ; i < listOfCboxIds.size() ; ++i) {
CheckBox cbox = (CheckBox) findViewById(listOfCboxIds.get(i));
if(cbox.isChecked()) {
// Do something
}
}
Keep the references to CheckBoxes and EditTexts in an ArrayList and just iterate over it. This is less CPU intensive.
List<CheckBox> yourCheckBoxes = new ArrayList<CheckBox>();
List<EditText> yourEditTexts = new ArrayList<EditText>();
try {
JSONObject json_row = new JSONObject(json.getString("row" + x));
CheckBox chb = new CheckBox(this);
chb.setText(json_row.getString("pregunta"));
chb.setId(json_row.getInt("pregunta_verificacion_supervision"));
chb.setTextSize(10);
chb.setPadding(8, 3, 8, 3);
chb.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
chb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(chb);
EditText et = new EditText(this);
et.setHint("observaciones");
et.setId(json_row.getInt("pregunta_verificacion_supervision"));
et.setTextSize(10);
et.setPadding(8, 3, 8, 3);
et.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
et.setInputTyp
e(android.text.InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(et);
yourCheckBoxes.add(chb);
yourEditTexts.add(et);
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
Once you need them
for(int i = 0 ; i < yourCheckBoxes.size() ; ++i) {
CheckBox cbox = (CheckBox) yourCheckBoxes.get(i);
EditText et = (EditText) yourEditTexts.get(i);
if(cbox.isChecked()) {
// Do something
}
}
Like Bojan Kseneman said the best option is to store your Views in a Data Structure. This could look like the following:
List<CheckBox> checkBoxes = new ArrayList<>();
List<EditText> editTexts = new ArrayList<>();
Then you add each box and editText to these Lists and later on you iterate over them like this:
for(int i = 0; i < checkBoxes.size(); i++){
CheckBox box = checkBoxes.get(i);
EditText editText = editTexts.get(i);
doSomeThingWithThem();
}
Hope this helps.

Creating an XML for Dynamically created Elements

I want XML for the TextViews and EditTexts created dynamically. Some blogs suggest that there are some Third Party Libraries that can do that but I wasn't able to find one. I am basically creating TextViews & EditTexts dynamically in my code on a button click.
Code:
LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
for (int x = 0; x < 1; x++) {
Display display = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth() / 3;
TextView et1 = new TextView(this);
et1.setBackgroundColor(color.transparent);
et1.setText("Untitled");
et1.setGravity(Gravity.LEFT);
EditText et = new EditText(this);
et.setHint("Click to add");
et.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LayoutParams lp2 = new LayoutParams(width,
LayoutParams.WRAP_CONTENT);
// lp1.addRule(RelativeLayout.BELOW, et1.getId());
linearLayout1.addView(et1, lp2);
linearLayout1.addView(et, lp2);
XML:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/addImage" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/addEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/scrollView1"
android:text="Edit" />
The question is, how do I achieve the XML for the TextView & EditText as a String value? Do I give them tags and ids statically in the code or is there any other way?
public static final void writeMapXml(Map val, String name, XmlSerializer out)
throws XmlPullParserException, java.io.IOException
{
if (val == null) {
out.startTag(null, "TextView");
out.endTag(null, "TextView");
return;
}
Set s = val.entrySet();
Iterator i = s.iterator();
out.startTag(null, "TextView");
if (name != null) {
out.attribute(null, "name", "TextView");
}
out.endTag(null, "TextView");
}
XMLSerializer provided by android itself is enough to make dynamic XMLs.

How to add padding to a tabs label?

I just started programming for android. I'm using a tab based layout in my app. I would like to put some padding around the tab label so that it's not so close to the icon.
here is how the label is put in:
in main.java
spec = tabHost.newTabSpec("tab1").setIndicator(this.getString(R.string.tab1), res.getDrawable(R.drawable.ic_tab1)).setContent(intent);
and in string.xml
<string name="tab1">my tab label</string>
I've been searching and trying to figure this out for several hours now. I could just make the icons smaller in about two minutes but I like their size.
Can anyone suggest the best way to do simple formatting to the tab label?
tabHost.addTab(tabHost.newTabSpec("tab1").setContent(
new Intent(this, DealCities.class)).setIndicator(prepareTabView("Deals",R.drawable.deal)));
Where prepareTabView is a method.. In these method Inflate a view and add Image and Text as follows :
private View prepareTabView(String text, int resId) {
View view = LayoutInflater.from(this).inflate(R.layout.tabs, null);
ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
TextView tv = (TextView) view.findViewById(R.id.TabTextView);
iv.setImageResource(resId);
tv.setText(text);
return view;
}
Where tabs is the inflated view and its xml as follows :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:id="#+id/TabLayout" android:background="#drawable/tab_bg_selector"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center"
padding="5dip">
<ImageView android:id="#+id/TabImageView" android:src="#drawable/icon"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="#+id/TabTextView" android:text="Text"
android:paddingTop="5dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#color/black"
android:textAppearance="#style/TabTextViewStyle" />
</LinearLayout>
Now you can make Your Paddings as you like..
A simpler solution is :
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setPadding(10,10,10,10);
}
just use this code
TabWidget widget = mTabHost.getTabWidget();
for (int i = 0; i < widget.getChildCount(); i++) {
//adjust height
widget.getChildAt(i).getLayoutParams().height =40;
//adjust textview
TextView tv = (TextView) widget.getChildAt(i).findViewById(android.R.id.title);
if (tv == null) {
continue;
}
tv.setAllCaps(false);
tv.setTextSize(15);
//set padding
tv.setPadding(10,5, 10, 5);
}

Categories

Resources