image and text in horizontal scroll View (dynamically) - android

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);
}

Related

How to add image button and label on main button dynamically in android?

I want to create multiple buttons dynamically with label and another small button on it. For example:-
Need to add drawable in button right.I think there is no need to add extra button on button if there is no action for upper button . please see below code and try
LinearLayout parent = new LinearLayout(this);
parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0 ; i < 10 ; i++) {
Button b = new Button(this);
b.setText("Primary");
Drawable image = ContextCompat.getDrawable(getApplicationContext(), R.drawable.your_image);
image.setBounds(0, 0, 60, 60);
b.setCompoundDrawables(null, null, image, null);
parent.addView(b);
}
If you face any issue .. let me know.. hope this help
Ok so first of all make an item of Image button and label in xml.
item.xml
<LinearLayout
android:id="#+id/llOverallTitleDOWN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Then on java file refer the below code.
LinearLayout llOverallTitleDOWN = (LinearLayout) findViewById(R.id.llOverallTitleDOWN);
View layout2 = LayoutInflater.from(this).inflate(R.layout.item, llOverallTitleDOWN, false);
ImageButton imgBtn = (ImageButton) layout2.findViewById(R.id.imgBtn);
TextView textLabel = (TextView) layout2.findViewById(R.id.textLabel);
imgBtn.setImageBitmap(bitmap);
textLabel.setText("label");
llOverallTitleDOWN.addView(layout2);

How to make an XML element a blueprint to allow for re-usability?

My XML file is the following:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rants_list_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
I then have a res/layout/sonich.xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/sonich"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="32sp"
xmlns:android="http://schemas.android.com/apk/res/android"/>
My goal is to make the above TextView a reusable view, that is, reference it in the application multiple times to create text views of the properties specified in the above text view.
Hence, in the main activity, I do the following:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rants_list_linear_layout);
for (int i = 0; i< 100; i++) {
TextView txt = (TextView) findViewById(R.id.sonich);
linearLayout.addView(txt);
}
This, however, produces the following error:
java.lang.IllegalArgumentException: Cannot add a null child view to a
ViewGroup
This suggests that the findViewById() method returns a null reference. I do not understand why, since the text view is well defined in the xml file and the findViewById() method works well for the linear layout a few lines before that.
If I change the code to:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rants_list_linear_layout);
for (int i = 0; i< 100; i++) {
TextView txt = new TextView(this);
txt.setText("TextView");
txt.setTextSize(32);
linearLayout.addView(txt);
}
everything works fine. However, this is not what I want. I would like to outsource as much code to the xml files as possible. After all, the xml files exist to provide an organised way of specifying layouts, views etc. and their properties, whereas Java code is the one dealing with the app's functionality.
Could you please explain what this error stems from and how to create text views (or any views) programmatically, while referencing xml files to infer the views' properties?
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rants_list_linear_layout);
LayoutInflater inflater = LayoutInflater.from(linearLayout.getContext());
for (int i = 0; i< 100; i++) {
View sonich = inflater.inflate(R.layout.sonich, linearLayout, false);
TextView txt = (TextView) sonich.findViewById(R.id.sonich);
// Now you don't need to do these since the XML takes care of it
// txt.setText("TextView");
// txt.setTextSize(32);
linearLayout.addView(txt);
}

Dynammically create ImageViews not showing the image

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.

Add a scrollbar to a dynamic view

I have a LinearLayout and I amdynamically creating a certain number of TextViews in it.
Sometimes there are more TextViews than fit on the screen.
How do I add a scrollbar to this view, so the user can scroll up and down and see all TextViews?
Here's part of my code:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
for (int n = 0; n < (numberOfPlayers*(numberOfPlayers-1)/2); n++) {
TextView tv = new TextView(this);
tv.setText(gamelist[n][0] + " - " + gamelist[n][1]);
layout.addView(tv);
}
Include your linearLayout in a ScrollView
<ScrollView
android:id="#+id/scroll"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<LinearLayout ... />
</ScrollView>
Note that a ScrollView can only have one view child
Wrap your LinearLayout into a ScrollView.
In this case you may consider using a ListView (tutorial here).

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