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);
}
Related
I want to add dynamic view into a Horizontal LinearLayout. But my issue is if there is no space at Horizontal it should automatically placed on vertically.
My Image
My Expected Result
My Codes are
for (int j = 0; j < jobDet.getKeywords().length; j++) {
RelativeLayout tr_head = (RelativeLayout) getLayoutInflater().inflate(R.layout.tag_lay, null);
TextView label_date = (TextView) tr_head.findViewById(R.id.tag_name);
label_date.setText(jobDet.getKeywords()[j]);
keywordL.addView(tr_head, new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
}
My "tag_lay.xml"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:id="#+id/tag_name"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#drawable/round_edit_box"
android:text=" PHP "
android:gravity="center"
android:lines="1"
android:layout_marginRight="5dp"/>
</RelativeLayout>
You can not do that with single LinearLayout because it has either VERTICAL or HORIZONTAL orientation. I would suggest you to take a look at google's FlexboxLayout. Its a library which provides a layout where you can achieve similar view. You can add this library by adding below line to your app level gradle file:
compile 'com.google.android:flexbox:0.1.3'
You can change the keywordL to FlexboxLayout from LinearLayout. A sample code for container layout can be something like below:
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/flexbox_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:alignContent="flex_start"
app:alignItems="flex_start"
app:flexWrap="wrap"/>
You can change the value of alignContent, alignItems and flexWrap according to your requirement. Although this should work because it is working for me. While adding the childs you can do something like below:
for (int j = 0; j < jobDet.getKeywords().length; j++) {
RelativeLayout tr_head = (RelativeLayout) getLayoutInflater().inflate(R.layout.tag_lay, keywordL, false );
TextView label_date = (TextView) tr_head.findViewById(R.id.tag_name);
label_date.setText(jobDet.getKeywords()[j]);
keywordL.addView(tr_head);
}
Please let me know if you have trouble implementing.
I have a horizontal scroll view containing a linear layout.
Something like this:
<HorizontalScrollView
android:fillViewport="true"
android:id="#+id/product_hsv"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:measureAllChildren="false"
android:scrollbars="none">
<LinearLayout
android:id="#+id/product_container_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
What I want to do is to add n number of the same layout to display
product collection in a Horizontal scroll view.
The picture below will give you an idea of what I have done so far.
For this, I have added product collection view to R.id.product_container_layout.
String[] productCollectionArr = new String[]{"Shirt", "Jewellary", "Moto X Play", "Ellis Flat"};
for (int i = 0; i < productCollectionArr.length; i++) {
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.product_container_layout);
View child = getLayoutInflater().inflate(R.layout.item_product, null);//child.xml
parentLayout.addView(child);
}
for (int i = 0; i < productCollectionArr.length; i++) {
LinearLayout eachProductLayout = (LinearLayout) findViewById(R.id.product_container_layout);
((TextView) eachProductLayout.findViewById(R.id.product_name_tv)).setText(productCollectionArr[i]);
}
But the problem is passing values. Since each view added to product_container_layout has the same ids. Therefore only first element gets the value from the product collection array.
What I can do is generate view id for each view and its element and map these ids to certain name so that I could access them by id.
Is this the correct methodology or should I do something else?.
You need to add this first :
<HorizontalScrollView
android:id="#+id/galleryImages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/chim_image_view_divider"
android:layout_marginTop="#dimen/twenty"
android:focusable="false"
android:layout_centerInParent="true"
android:focusableInTouchMode="false"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/Images_scroller_view_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
ImageScrollerViewLinearLayout = (LinearLayout) findViewById(R.id.Images_scroller_view_linear_layout);
ImageScrollerViewLinearLayout.removeAllViews();
- addImagesTo list:
void addImages() {
ImageScrollerViewLinearLayout.removeAllViews();
int i = 0;
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = 0;
if (myImagesList != null && myImagesList.size() > 0) {
for (i = 0; i < myImagesList.size(); i++) {
ImageView imageView = new ImageView(this);
pixels = (int) (60 * scale + 0.5f);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pixels, pixels);
params.setMargins(5, 0, 0, 0);
imageView.setLayoutParams(params);
imageView.setScaleType(ScaleType.CENTER_CROP);
imageView.setOnClickListener(new SelectNewSingleImageClickListener());
imageView.setImageResource(myImagesList.get(i));
ImageScrollerViewLinearLayout.addView(imageView, i);
}
}
}
So, you need to populate your view before adding to container or save links to all items. It will looks like this:
LinearLayout parentLayout=(LinearLayout)findViewById(R.id.linearLayout1);
for (int i = 0; i < items.size(); i ++) {
View child = getLayoutInflater().inflate(R.layout.item_grid, null);//child.xml
((TextView) child.findViewById(R.id.name)).setText(items.get(i).getName(); // where R.id.name is a textView in your child.xml layout.
parentLayout.addView(child);
}
after all, you can access to your elements by index, if you want to change it:
parentLayout.getChildAt(index)
But if you have a lot of items, it's not good idea. In this case you will not recycle your views and will consume a lot of memory.
I suggest to use recyclerview from support library.
add code in xml file
<org.lucasr.twowayview.TwoWayView
android:id="#+id/lvItems"
style="#style/TwoWayView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#+id/linearLayout3"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/transparent"
android:drawSelectorOnTop="false"
android:padding="10dp" >
</org.lucasr.twowayview.TwoWayView>
You can use **DevSmart** library for horizontal listview which is similar to **ListView**.
https://github.com/dinocore1/DevsmartLib-Android
I have a layout where I want to imitate a listview by adding items programmatically one below the other. So I created an .xml for the layout of these items that are something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_lista"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="#+id/rellay_btn"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:text="Some name"
android:textColor="#3F3F3F"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/iv_follow"
android:text="Some text"
android:textColor="#3F3F3F"
android:textSize="13sp" />
</LinearLayout>
In the activity I would like to generate n of this layout, fill the textviews with text and also I would like to set an onClickListener on them. n is the size of an array. Please help
Note: Once the activity loads, the number of layouts will not change, nor can a layout be removed.
This is what I have now but the layout is displayed on top of the activity instead of below a Textview:
LayoutInflater inflater = LayoutInflater.from(BucketProfileActivity.this);
for (int i = 0; i < 3; i++) {
View view = inflater.inflate(R.layout.bucketprofile_tips, null);
view.setId(i);
TextView tv_username = (TextView) view.findViewById(R.id.tv_username);
tv_username.setText(String.valueOf(i));
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.BELOW, R.id.tv_nemkell);
addContentView(view, rl);
}
If your posted xml file is bucketprofile_tips.xml then you need to change here from
TextView tv_username = (TextView) view.findViewById(R.id.tv_title);
to
TextView tv_username = (TextView) view.findViewById(R.id.tv_username);
Because in bucketprofile_tips.xml tv_title id not found for your TextView. So you are getting Null Pointer Exception.
Not sure why you would want to do this but you can just use a LayoutInflater to inflate x of this view and add them to a designated ViewGroup. For example:
LayoutInflater inflater = LayoutInflater.from(context);
for (int i = 0; i < numberOfViews; i++) {
View v = inflater.inflate(R.layout.view_to_add, parentView, false);
setup(v);
parentView.addView(v);
}
Where setup() is a function you define that does the setup work such as setting the text of a TextView etc.
For instance, in your setup function might look something like this:
private void setup(View v) {
TextView tv = (TextView) v.findViewById(R.id.name);
// set the text of the text view
tv.setText("Hello!");
// set the click listener for the view...
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do stuff here...
}
});
}
LinearLayout parentLayout=findViewById(R.id.parentLayout);
LayoutInflater inflater = LayoutInflater.from(BucketProfileActivity.this);
View view = inflater.inflate(R.layout.bucketprofile_tips, null);
parentLayout.addView(view);
LinearLayout textList=(LinearLayout) parentLayout.getChildAt(0);
for(int i=0; i<textList.getChildCount(); i++){
TextView tv_username=(TextView) textList.getChildAt(i);
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
tv_username.setText(String.valueOf(i));
}
}
}
I want to have in a horizontal Scroll View images and below each image i want a Text View. I want this dynamically. Until now i have Buttons with background for image, i use the method set Text for the text and it works. But, i want to edit the text separately from image, because i want a different color for text in order to be visible. This is my xml:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/ImagesHsw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
update:
vi= ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.actor,null);
Button b = (Button)findViewById(R.id.actorImage);
TextView text = (TextView)findViewById(R.id.actorName);
for (int i=0; i<actors; i++) {
b.setBackgroundResource(R.drawable.actor);
b.setLayoutParams(new ViewGroup.LayoutParams(160,150));
text.setText(i);
actorsScrollView.addView(vi);
}
have separate xml file with image and text as u expect.
Then add dynamically using inflate
for (int i = 0; i < actors; i++) {
View vi = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.actor, null);
Button b = (Button) vi.findViewById(R.id.actorImage);
TextView text = (TextView) vi.findViewById(R.id.actorName);
b.setBackgroundResource(R.drawable.actor);
text.setText(i);
actorsScrollView.addView(vi);
}
I have a custom adapter for a listview and here is the getView method;
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) JourneyPlannerActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.journey_planner_route_detail, viewGroup, false);
LinearLayout layout = (LinearLayout) rowView.findViewById(R.id.journey_planner_detail_detail_main_id);
JourneyPlannerRoute r = m_Routes.get(i);
String directions = "";
for(int j=0 ; j < r.getRoutes().size() ; j++){
ImageView image = new ImageView(JourneyPlannerActivity.this);
String transportMethod = r.getRoutes().get(j).getMeansOfTransport();
if(transportMethod.equals("Train"))
image.setImageResource(R.drawable.network_rail_logo);
else if(transportMethod.equals("Subway"))
image.setBackgroundResource(R.drawable.roundel_tube);
else if(transportMethod.equals("Bus"))
image.setBackgroundResource(R.drawable.bus);
else if(transportMethod.equals("Walk"))
image.setBackgroundResource(R.drawable.walking);
image.setVisibility(View.VISIBLE);
layout.addView(image);
//directions += r.getRoutes().get(j).getMeansOfTransport()+",";
}
directions += " "+r.getDuration();
TextView tv = (TextView) rowView.findViewById(R.id.journey_planner_detail_main_text_view);
tv.setText(directions);
return rowView;
After going through the debug it seems the imageviews are being added to the layout but they're just not appearing on the screen;
I had a feeling it was due to not picking up the correct layout but it seems as though it is?!
Here is the xml file for the row;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/journey_planner_detail_detail_main_id">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bus_small"/>
<TextView
android:id="#+id/journey_planner_detail_main_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Instead of creating new Image view just call rowView.findViewById(R.id.image_id); (you'll have to add id to the xml).
The actual bug is with the TextView. It is set to fill_parent causing the newly added ImageView to appear to the right of the visible screen. You can change this by changind the TextView width to wrap_content. However, using the ImageView defined in xml layout is better.