Android Individual Tab Colors - android

I have created four tabs but want each tab to be a different block color - should this be done in the xml or java file? I'm assuming java because I only have one xml file for the whole tab widget.

try to customise the individual tabs
try using something like
TabWidget tabWidget = getTabWidget();
for(int i = 0; i < tabWidget.getChildCount(); i++) {
RelativeLayout tabLayout = (RelativeLayout) tabWidget.getChildAt(i);
tabLayout.setBackgroundDrawable(someimage);
}

Related

Xamarin dynamically added views using inflater inside loop adding only one entry

I followed the below link to dynamically add a layout multiple times using inflater and AddView()
Is there a way to programmatically create copies of a layout in android?
I used a loop to create multiple entries. But only one entry is comming up which is the result of last loop index
Below is my C# code
I can see only one child inside the parent which is the result of last loop.
What I missed?
var parent = FindViewById<RelativeLayout>(Resource.Id.ParentLayoutWrapper);
for (int i = 0; i < 4; i++)
{
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, parent, false);
var txtView = view.FindViewById<TextView>(Resource.Id.textViewSample);
txtView.Text = i.ToString()+ " Android application is debugging";
txtView.Id = i;
parent.AddView(view, i);
}
The original post you worked from had a LinearLayout as the parent layout, not a RelativeLayout like you have. When you add a view (or another layout) to a LinearLayout, it gets positioned below (when LinearLayout has vertical orientation) any existing elements in the layout. However, the elements in a RelativeLayout need to use positioning properties to determine where they will be in the RelativeLayout, so every time you add the new layout, RepeatingLayout, since you are not changing the layout options, the view/layout is added over the existing view/layout. So change the parent layout to a LinearLayout in your layout file and then this should work:
LinearLayout parent = FindViewById<LinearLayout>(Resource.Id.parentLayout);
for (int i = 0; i < 4; i++)
{
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, null);
var tv = view.FindViewById<TextView>(Resource.Id.textViewSample);
tv.Text = i.ToString() + " Android application is debugging";
parent.AddView(view);
}
Trying to do the same with a RelativeLayout as the parent layout highly complicates things unnecessarily.

Setting Some TextView's Text

In my Project , I have 80 TextViews.
I should set their text from 1 to 80 once project runs , and they dont need to be changed in future.
Except TxtViews , I have some other things in my Layout, the TextViews are under ImagesViews. actually I have 80 imagesViews and under them are 80 TextViews. I want to set text of textViews from 1 to 80 dynamically.
I know I can do it in my layout.xml ,
but its really time consuming.
is there any way to do that by code?
for example with a for cycle or something like that?
Create a ViewGroup suitable for your needs in the layout, for example:
<LinearLayout
android:id="#+id/linear_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Then you create you TextView instances programatically, and add them to the LinearLayout, like this:
LinearLayout layout = (LinearLayout)findViewById(R.id.linear_layout);
for(int i = 0; i < 80; i++) {
TextView textView = new TextView(getContext());
textView.setText("text" + i);
layout.addView(textView);
}
Optionally, you can add tags or whatever to locate them again. Alternatively just iterate over the layouts subviews.
If you know that 80 Textview fixed then you should take listview for that.
Listview Benefit
Memory management automatically
Listview manage indexing
If they share the same layout, except for the text, and could be displayed as a list, you could use an ArrayAdapter and pass the values from code.
http://www.mkyong.com/android/android-listview-example/
Checkout the below example,
public class MainActivity extends Activity {
LinearLayout linearLayout ;
ScrollView scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (HorizontalScrollView) findViewById(R.id.scrollViewActivityMain);
}
private void populateTextViews() {
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//add all textViews here
for(int i=0; i < 80; i++){
TextView myTextView = new TextView(this);
myTextView.setText("My TextView "+i);
myTextView.setGravity(Gravity.CENTER);
linearLayout.addView(myTextView);
}
scrollView.addView(linearLayout);
}
}
Don't forget to put that scrollView in your xml.
Let me know if it works for you...
If your TextViews are declared on the xml, wrap them on another view so you can reference it on the java code later, then simply use a for.
Something like:
View view = findViewById(R.id.your_wrapper);
for(int i=0; i<((ViewGroup)view).getChildCount(); i++) {
View nChild = ((ViewGroup)view).getChildAt(i);
TextView tv = (TextView) nChild;
tv.setText(String.valueOf(i + 1));
}
If not, you can simply create them dynamically inside your java code, and append them to a layout like LinearLayout.
Example:
xml
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear"
/>
Java code
LinearLayout ll = (LinearLayout) findViewById(R.id.linear);
for (int i = 1; i <= 80; i++) {
TextView tv = new TextView(this); // Assuming you're inside an Activity.
int count = ll.getChildCount();
tv.setText(String.valueOf(i));
ll.addView(tv, count, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
EDIT: But truly, you should use RecyclerView or ListView for that if your values are not going to change.
You can read more about RecyclerView here, and on ListView here.
Second edit: From what you're saying on your comments, you REALLY should be using ListView instead of your current design. The solutions above and from the other answers won't work at all for your problem.

I can't add several TextViews in a Layout from method

I'm trying to create a menu list from json data in a LinearLayout using the follwing code:
LinearLayout myLayout = (LinearLayout)findViewById(R.id.info);
for (int i = 0; i < jsonArray.length(); i++) {
try {
jsonObj = jsonArray.getJSONObject(i).getJSONObject("store");
textView = new TextView(context);
textView.setText(jsonObj.getString("name"));
textView.setId(jsonObj.getInt("id"));
myLayout.addView(textView);
} catch (JSONException e) {
e.printStackTrace();
}
}
It works, but only until the third record, after that all the text views are not shown.
I think there may be a limit or something that doesn't allow me to add more textviews.
Any idea?
I think first you should make your layout linear and vertical. Do its orientation vertical in the xml.
android:orientation
You're better of using a ListView and ArrayAdapter, as far as I know addView is no longer supported, and throws an exception when used in recent versions of Android.
This is a great tutorial on the subject.

Wrapping a scrollview in a tablelayout in XML

So, I create table view in XML to which the rows are added dynamically using the following java code:
private void fillTable(TableLayout tableLayout, String[] items) {
for (int i = 0; i < items.length; i++) {
TextView itemText = new TextView(FillingActivity.this);
itemText.setText(items[i]);
TableRow row = new TableRow(FillingActivity.this);
row.addView(itemText);
if(i % 2 == 1)
row.setBackgroundColor(color.LightGreen);
tableLayout.addView(row);
}
}
The code for a single tablelayout is the following:
<TableLayout
android:id="#+id/dinnertableviewing"
android:layout_width="346dp"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="210dp" >
</TableLayout>
But then when add many items to that table it overlaps with the space for the next tables
here is a picture that shows it:
Don't use AbsoluteLayout. The difficulties you're running into when an AbsoluteLayout contains content that can resize or when you run on differently sized screens (e.g. just about every Android device in the wild) are exactly why AbsoluteLayout is deprecated.
If you're trying to position elements vertically down the screen as they fit, use a LinearLayout with android:orientation="vertical".

Successive ImageViews in a loop

I am working on an app where I have to set up a number of drawables. The number depends on the user input.
I therefore need to use a loop there I set up as many successive images as the user have chosen in one or several rows.
I can not find out how. I know how to put the images in an array and use
(ImageView) findViewById(R.id.img)
to set them up. But I want to create this successive ImageViews in the loop.
try to make a combinatino of LinearLayout and LayoutInflater (to inflate custom views)
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
for (int i = 0; i < userChoice; i++)
{
LayoutInflater li = LayoutInflater.from(yourContext);
View customView = li.inflate(R.layout.image_holder, this);
// (In case you need to use the image) ImageView yourImage = (ImageView)customView.findViewById(R.id.image_view);
layout.addView(customView);
}
where:
userChoice is the number the user has choose
layout is linear layout inside your main.xml (or the root element itself)
image_holder is an xml with ImageView inside it

Categories

Resources