Use addView instead of a listView - android

I'm trying to implement a code where there are multiple items.
I am able to populate the ListView and display all these items but I want to inflate a view onItemClick. Unfortunately, AdapterView does not support addView().
So now I want to use addView() to dynamically add items by traversing an ArrayList<>().
How do I do it?
Basically, I want to display a Custom Keypad layout next to a TextView inside the ListView and update the Value of the TextView
This is what I'm trying to do now.
public class OrderSummary extends Activity {
LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_summary);
linearLayout = new LinearLayout(this);
View view = View.inflate(this, R.layout.top_bar_layout, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.addView(view, params);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(OrderSummary.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}

What you can do is take a LinearLayout(with orientation set to vertical) instead of a ListView. Then make a custom view for each row, traverse the ArrayList, and then inflate the views and keep adding them to the LinearLayout with addView().
P.S. without the actual code , I cannot tell you how to correct your code.

In your code there is issue where are you adding linearLayout into main container ?

Related

Add multiple custom LinearLayouts with Buttons to ListView item

I have a question related to this one.
Someone asked about adding multiple custom LinearLayouts programmatically to ListView item and this is almost what I need to do. In this question the author has TextViews in a LinearLayout. I have a Button (and some others) and when I click on this Button I want to inflate a new LinearLayout under this one with just clicked Button.
How to get current LinearLayout position so I could add new LinearLayout under it? Now I'm adding new LinearLayout at the bottom of the list.
You can do like this
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//if you want get linearLayout at current position
LinearLayout LINTOP = ((LinearLayout) parent.getChildAt(0));
addLayout(LINTOP);
}
});
add new layout like this under LINTOP
public void addLayout(LinearLayout parent){
LinearLayout newLayout= new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parent.addView(newLayout, 0, params);
}
First of all, please use RecyclerView instead of ListView
You have a method called getAdapterPosition() which returns the position where the interaction happened. So in the ViewHolder if you so something like
linearLayoutView.setOnClickListener
{
Log.d("Position:"+getAdapterPostion());
}
That would give you the position. (The Above code is in Kotlin btw, so may look a bit different)

inflates 2 views using same xml:second view contains properties of first view

When changing the text of TextView of the first view, the textView's text of the second view shows the text of both TextViews, one on top of the other.
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout rl=(FrameLayout) findViewById(R.id.mainLayout);
View v1=LayoutInflater.from(this).inflate(R.layout.text_view, null);
View v2=LayoutInflater.from(this).inflate(R.layout.text_view, null);
TextView myTextView1= (TextView) v1.findViewById(R.id.myTextView);
TextView myTextView2= (TextView) v2.findViewById(R.id.myTextView);
myTextView1.setText("str1");
myTextView2.setText("str2");
rl.addView(v1);
rl.addView(v2);
}
I would guess the issue is because you are using a framelayout so the subviews will overlap. If you switch that framelayout to a linearlayout for example I imagine the results will be as you expect according to the textviews.
very simple.. 1. you have to use append property rather than settext in second textview
2nd you can use extras to pass using intent or use getter setter method like
public static void setstr_name(String str_name) {
GlobalVariable.str_name = str_name;
}
public static String getstr_name() {
return str_name;
}

Horizontal ListViews inside a Vertical ScrollView are running slow

i am making an app which is similar to pulse and BBC News app
there are titles and beneath each title there is a horzintally scrolling image and text data
i done the horzintal listview part with this library: http://www.dev-smart.com/archives/34
now everything is fine but the problem is the list is scrolling very slowly in the vertical way (its fine in the horizontal way in each row)
how can i solve this problem?
this is my function that adds the horizontal and the title part to the layout:
public void AddCategory(String CatTitle, List<CItem> ItemList) {
LinearLayout thelayout = (LinearLayout) findViewById(R.id.mainlayout);
HorizontialListView listview = new HorizontialListView(this, null);
listview.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, getPixels(100)));
listview.setBackground(getResources().getDrawable(
R.drawable.categoryitemsbackground));
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
TextView tw;
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ///////////////////////////////////////////DO SOMETHING AFTER
// CLICK PART ////////////////////////////////////////////
tw = (TextView) view.findViewById(R.id.itemtitle);
Toast.makeText(getApplicationContext(),
tw.getText().toString(), Toast.LENGTH_LONG).show();
}
});
CategoryAdapter mycategories = new CategoryAdapter(this,
R.layout.categoryitem, ItemList);
listview.setAdapter(mycategories);
TextView textview=(TextView)getLayoutInflater().inflate(R.layout.categorytitle, null);
textview.setText(CatTitle);
thelayout.addView(textview);
thelayout.addView(listview);
}
In the BBC News app, they are not using HorizontalListView instead they are using Gallery. Please try using it.
Why don't you use view pagers? They works better than Horizontal ListView and they aren't deprecated like Gallery.

how to display new activity over another activity?

i have an activity that displays two buttons , on click of call button i want to show another activity as shown in image.
Please help .How to achieve this
One way to do it is to have a custom layout which you add to each activity with a viewgroup as such:
private View view;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup parent = (ViewGroup) findViewById(R.id.linearLayout1);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.custom_layout,
null);
parent.addView(view);
Then you just need to implement your listeners and whatnot further down :)
You need to create xml file of these two buttons and add that new xml into old one by using Layout Inflator. for e.g
1) your new xml
LayoutInflator buttons = LayoutInflater.from(getBaseContext()).inflate(R.layout.buttons,
currentxml, false);
2) your old xml parent referenced through id -->
RelativeLayout relativeLayout = (RelatveLayout) findViewById(R.id.oldxml);
3) now add..
relativeLayout.addView(buttons);
You can Use Pop up By Adding this in your function. Popuser is a xml file which you want to inflate.
public void popupshow(){
try{
LayoutInflater inflater=(LayoutInflater)SRSDMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth()/2;
int height=display.getHeight()/2;
View pop=inflater.inflate(R.layout.popupuser,null,false);
pop.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);
height=pop.getMeasuredHeight();
width=pop.getMeasuredWidth()+50;
pu=new PopupWindow(pop,width,height,true);
pu.showAtLocation(findViewById(R.id.ll3),Gravity.CENTER,1,1);
Button btn=(Button)pu.getContentView().findViewById(R.id.button);
btn.getBackground().setColorFilter(new LightingColorFilter(0xFF505450,0xFF101010));
btn.setTextColor(Color.WHITE);
btn.setTypeface(null,Typeface.BOLD);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//anything you want to do
pu.dismiss();
}
});
}
catch(Exception ex){
//Catch The Exception
}
}
}

Search Textview which is created programmatic

I'm adding TextViews programmatic using a for-loop and use setId(Int int) to set unique id for that perticular Textview.
But now I want to search textView on the basis of that id.
How can I search?
erorr occurd 'app stopped unexpectedly...' Here is my code.
public class Idtest extends Activity {
TextView tv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll=new LinearLayout(this);
tv=new TextView(this);
tv.setText("Hello");
ll.addView(tv);
tv=new TextView(this);
tv=(TextView)ll.getChildAt(1);
setContentView(tv);
}
}
tv is obviously null at the end (the LinearLayout only has one child, which is at index 0), so you're basically calling setContentView(null) which results in an exception. It's not clear for me what you're trying to do (your code is pretty messed up).
Supposing you are trying to show multiple TextViews in a LinearLayout, here's my suggestion:
public class Idtest extends Activity {
LinearLayout mainLayout;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout = new LinearLayout(this);
setContentView(mainLayout);
for (int i=0; i < 10; i++) {
TextView tv = new TextView(this);
tv.setText("Hello " + i);
mainLayout.addView(tv);
}
}
}
If, at any later point, you need one of the TextViews, do:
TextView tvX = mainLayout.getChildAt(X); // where X is between 0 and 9
Also, please note that creating layout from code is evil. If you can avoid it, please do. For example, if the number of TextViews is dynamic, then it's perfectly normal to create them from code (although you could inflate them). However, it's not advisable to also create the LinearLayout from code. You should have that in an XML. If it's possible for you to also have the TextViews in an XML, that would be even better.
There is another option too...we have setTag() and getTag() methods. While adding a textview give a tag for it and whenever u want to do a search use getTag().
I suppose you are adding your TextView controls to some ViewGroup (e.g. LInearLayout)?
You can iterate through the ViewGroup child views using getChildCount() / getChildAt(index) and compare the child view id with the one you're searching for.

Categories

Resources