Removing Single View from a layout programmatically - android

I'm trying to remove a single view from layout but it is not being removed.
But, when i call removeAll, it does remove them all.
Here is the code:
for (int subspe = 0; subspe < totalSubSpe; subspe++) {
View childSubSpe =
BasicInfoUpdateDocProfile.ll_custom_for_supspecialist.getChildAt(subspe);
TextView subSpecialityId = (TextView) childSubSpe.findViewById(R.id.tv_speciality_id);
TextView specialistId = (TextView) child.findViewById(R.id.tv_id);
String speID = specialistId.getText().toString();
String subSepId = subSpecialityId.getText().toString();
Log.e("TAG", "the the the the id is: speciality " + speID);
Log.e("TAG", "the the the the id is: sub speiciality " + subSepId);
if (speID.equals(subSepId)) {
//removing sub qualificaiton
Log.e("TAG", "It is Equal subspecialist ");
ViewGroup par = (ViewGroup) childSubSpe.getParent();
par.removeView(childSubSpe);
//BasicInfoUpdateDocProfile.ll_custom_for_supspecialist.removeView(childSubSpe);
BasicInfoUpdateDocProfile.ll_custom_for_supspecialist.
removeView((View)childSubSpe.getParent());
// BasicInfoUpdateDocProfile.ll_custom_for_supspecialist.removeAllViews();
}
}

Related

getBackground() return null

So I'm trying to hook OnActivityResume using Xposed. This is one part of my code in after hook:
ArrayList<View> views = new ArrayList<View>();
listViews(views, rootView);
Log.d("ViewListing", Integer.toString(views.size()));
for(Iterator<View> i = views.iterator(); i.hasNext(); ) {
final View item = i.next();
Log.d("ViewListing", "View:" + item.getClass().getName() + ":" + Boolean.toString(item instanceof android.support.v7.widget.Toolbar));
if(item.getClass().getName().equals("android.support.v7.widget.Toolbar")) {
Log.d("ViewListing", "Found Toolbar!");
Drawable d = (Drawable) XposedHelpers.getObjectField(item, "mBackground");
Log.d("ViewListing", "And background?" + d);
}
}
The toolbar is found, but the drawable is null. (My goal is to get the background color)

Android : How to get Dynamic EditText values

In my application I'am creating 10 EditText by dynamically. Now I want to give different value in run time and I want to add it to the list. I have assigned EditText object to the String variable like object.getText.toString(). But i cant get any value.I'am a beginner in android. Can anyone help me how to achieve this? Thanks in advance.
for(int i=0;i<=10;i++)
{
requirement = require.get(i);
RelativeLayout rl1 = new RelativeLayout(getActivity());
rl1.addView(req1);
req1estimate_value = new EditText(getActivity());
String value = req1estimate_value.getText().toString();
rl2.addView(req1estimate_value);
}
Try this. You should instantiate relative layout (rl1) at out of for loop, and should add child views with in that, so that all views could belongs to a parent layout. After that for accessing the values of all EditText you can use following:
String viewValue;
ViewGroup rootView = (ViewGroup) rl1;
int count = rootView.getChildCount();
for (int i = 0; i < count; i++) {
View view = rootView.getChildAt(i);
if (view instanceof EditText) {
viewValue = ((EditText) view).getText().toString();
Log.v("Value:: ", i + " " + viewValue);
} else if (view instanceof Spinner) {
viewValue = ((Spinner) view).getSelectedItem()
.toString();
Log.v("Value:: ", i + " " + viewValue);
}
}
Now after getting values you can put on a List or anywhere you want to use.

How to add dynamic layout in existing layout ( Android, JSON )

I have an app in which I am showing data from JSON. I am displaying data in a dynamic textview on the right and left side of the relative layout. Now I want to add this layout in an existing layout so that I can apply an OnClickListener on the textview. Right now I am getting data into a string and then setting that string into static textviews in the left and right side of the layout.
How would it be possible to generate textview dynamically on the basis of number of data I am getting from JSON ?
for (Region object : temp.phonelist.regionList)
{
if (object.getCCInfoShortDesc() != null || !(object.getCCInfoShortDesc().equals(null)))
{
Log.i("nullexception", "nullexception");
holder.tvDescription.setText(object.getCCInfoShortDesc());
holder.tvDescription.setVisibility(View.VISIBLE);
}else {
Log.i("nullexception1", "nullexception1");
holder.tvDescription.setVisibility(View.GONE);
}
leftContent += object.getCCInfoLeft() + ":" + "\n";
rightContent += object.getCCInfoRight() + "\n";
}
Log.i("lefftcontent", leftContent);
Log.i("rightcontent", rightContent);
if (leftContent != null) {
holder.tvData2.setText(leftContent);
holder.tvData2.setVisibility(View.VISIBLE);
}
if (rightContent != null) {
holder.tvData1.setText(rightContent);
holder.tvData1.setVisibility(View.VISIBLE);
}
You can do it in this way..
final int Count = < Number of TextViews>; // total number of textviews to add
final TextView[] TextViewsARR = new TextView[N]; // create an empty array;
for (int i = 0; i < Count; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is row #" + i);
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
// save a reference to the textview for later
TextViewsARR [i] = rowTextView;
}
I have a sample below that generates a checkbox dynamically, If you
observe i am generating the checkbox based on the cursor count.
You can adapt this saple to your needs
Instead of checkbox use a texview
Give any layout like linear, relative etc and generate views
dynamically
private CheckBox chkBoxMealType[] = null;
mCursor = db.rawQuery("SELECT meal_type_id,meal_type_name FROM meal_type_mas", null);
if(mCursor.moveToFirst()){
do{
if(chkBoxMealTypeCnt==0){
chkBoxMealType=new CheckBox[mCursor.getCount()];
}
//create a general view for checkbox
chkBoxMealType[chkBoxMealTypeCnt]= new CheckBox(getActivity());
//Create params for veg-checkbox
//Reason:: we don't need to worry about the data exist in cuisine_type_mas table
chkBoxMealType[chkBoxMealTypeCnt].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
chkBoxMealType[chkBoxMealTypeCnt].setTextColor(getResources().getColor(R.color.searchGoldLight));
chkBoxMealType[chkBoxMealTypeCnt].setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
chkBoxMealType[chkBoxMealTypeCnt].setTag(mCursor.getString(mCursor.getColumnIndexOrThrow(meal_type_mas.COLUMN_MEAL_TYPE_ID)));
chkBoxMealType[chkBoxMealTypeCnt].setText(WordUtils.capitalizeFully(mCursor.getString(mCursor.getColumnIndexOrThrow(meal_type_mas.COLUMN_MEAL_TYPE_NAME))));
mealTypeContainer.addView(chkBoxMealType[chkBoxMealTypeCnt]);
//since cursor count starts from 0 last count must be allowed
chkBoxMealTypeCnt++;
}while(mCursor.moveToNext());
Log.d("", "");
}
I have another sample..... Download this project(Click Here) and run in your editor
Snapshot::
Firstly you need to add a View in your layout ... Like you may try using LinearLayout or HorizontalLayout ... and then attach/add your dynamic textview to that layout.
Pragmatically you may try like this
packageButtons = new ArrayList<TextView>(); // Create your textview arraylist like this
for(int a = 0; a < your_text_view_from_json.size(); a++){
final TextView rowTextView;
rowTextView = new TextView(getApplicationContext());
rowTextView.setText(taxi_type_spin.get(a).taxi_type);
rowTextView.setTextSize(15);
rowTextView.setTextColor(getResources().getColor(R.color.white));
LinearLayout.LayoutParams lparam = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
//rowTextView.setPadding(0, 0, 0, 0);
packageButtons.add(rowTextView);
rowTextView.setLayoutParams(lparam);
rowTextView.setId(a);
final int b = a;
// get value of clicked item over here ..
rowTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button btn = (Button)v;
String get_value = btn.getText().toString();
//Toast.makeText(getApplicationContext(), "Button name is : " + get_value + " AND ID IS : " + rowTextView.getId(), Toast.LENGTH_LONG).show();
if(taxi_type_spin.get(b).taxi_type.equalsIgnoreCase(Utils.Hourly_Package))
{
setTaxiType(rowTextView.getId(),true);
ll_spin.setVisibility(View.VISIBLE);
}
else
{
setTaxiType(rowTextView.getId(),false);
ll_spin.setVisibility(View.GONE);
}
setSelectedButtonColor(b);
}
});
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
NOTE rowTextView .. this is your default view attached to your XML file
Hope it helps!
private void setLayout(LinearLayout llayout,
final ArrayList<String> items) {
for (int i = 0; i < items.size(); i++) {
LinearLayout row = null;
LayoutInflater li = getLayoutInflater();
row = (LinearLayout) li.inflate(R.layout.custom_item,
null);
ImageView image = (ImageView) row.findViewById(R.id.image);
llayout.addView(row);
}
}
I would suggest you to use ArrayList, because the class java.util.ArrayList provides resizable-array, which means that items can be added and removed from the list dynamically.
and get value from ArrayList something like this:
for(int i=0; i<arrayList.size(); i++)
{
textName.setText(object.getName());
}
How it is possible to genrate textview dynamically
on the basis of number of data i am getting from json.
You need to create TextView and add it to the parent layout each time you iterate on the forloop. So you will have textView for each of the element of the temp.phonelist.regionList
sample:
for (Region object : temp.phonelist.regionList)
{
TextView tx = new TextView(context); //creating a new instance if textview
//yourstuff goes here
tx.setText(text_you_want);
yourView.addView(tx); //this is to add the textView on each iteration
}
here is your solution do this way,Take one Layout(Linear or Relative) and add control dynamically....
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < 4; i++)
{
TextView txtDemo = new TextView(getActivity());
txtDemo .setTextSize(16);
txtDemo .setLayoutParams(lp);
txtDemo .setId(i);
lp.setMargins(0, 10, 0, 0);
txtDemo .setPadding(20, 10, 10, 10);
txtDemo .setText("Text View"+ i);
linearlayout.addView(txtDemo );
}
}

I can not break up the text on the page

I've got a question. text. for example
String str = "line1"+"\n" +
"line2"+"\n" +
"line3"+"\n" +
"line4"+"\n" +
"line5"+"\n" +
"line6"+"\n" +
"line7"+"\n" +
"line8"+"\n" +
"line9"+"\n" +
"line10"+"\n" +
"line11"+"\n" +
"line12"+"\n" +
"line13"+"\n" +
"line14"+"\n" +
"line15"+"\n" +
"line16"+"\n" +
"line17"+"\n";
I have a component that creates the page.
I need my text is divided into blocks (pages) and place it on the page.
Now I work like that. I break the text into lines, and each page can place the line. But I need to break up the text such as 20 lines per 1 page. and these blocks of 20 lines displayed on a separate view. Now I work as follows:
LayoutInflater inflater = LayoutInflater.from(this);
List<View> pages = new ArrayList<View>();
String[] str1 = str.split("\n");
View[] page1 = new View[7];
TextView[] textView1 = new TextView[7];
for (int i=1;i<page1.length;i++){
page1[i] = inflater.inflate(R.layout.page, null);
textView1[i] = (TextView) page1[i].findViewById(R.id.text_view);
textView1[i].setText(str1[i+1]);//then I do not need one row and 20
pages.add(page1[i]);
}

Android: Add programmatically multiple views to linear layout

I'm not sure how to handle that. I need linear layout which needs to have 20+ TextView and EditText components. I can define ImageView and buttons on top but I don't know how to generate rest of the components below and then put a button at the end.
It will look something like this:
---Button-----Button-------
--------ImageView----------
TextView -------- EditText
Item1.............[-------]
Item2.............[-------]
Item3.............[-------]
Item4.............[-------]
.
.
.
------------Button------------
There will be really a lot of components so I would like to avoid defining all components by hand in XML. After clicking on button all components need to be saved in pairs "name":"value" ("Item1":"EditText value"). I have a list of item's names and user will write the values for those items and save them to JSON file.
Thank you in advance.
you have to implement a class like this.
public class DetailedTicketSystemView extends LinearLayout
{
static int inc = 100;
ArrayList<XmlScommessaInCorso> _viewData;
ArrayList<xmlSistemaMovimenti> _systems;
Context _context;
public DetailedTicketSystemView(Context context, int res, AttributeSet attrs, ArrayList<XmlScommessaInCorso> viewData, ArrayList<xmlSistemaMovimenti> systems)
{
super(context, attrs);
_context = context;
_viewData = viewData;
_systems = systems;
LayoutInflater.from(context).inflate(res, this, true);
setId(inc);
inc++;
setLayout();
}
private void setLayout()
{
TextView textView;
String text;
for (int i = 0; i < _systems.size(); i++)
{
View quotesView = (View) LayoutInflater.from(_context).inflate(R.layout.row_ticket_detail_system_layout, null, true);
quotesView.setId(i);
// System Id
textView = (TextView) quotesView.findViewById(R.id.systemId);
text = "" + _context.getString(R.string.id_sistema) + " " + _systems.get(i).getAttributeOrVoidString(xmlSistemaMovimenti.ATTR_id) +
" / " + _viewData.size();
textView.setText(text);
// System Columns
textView = (TextView) quotesView.findViewById(R.id.systemColumns);
text = "" + _context.getString(R.string.colonne) + " " + _systems.get(i).getAttributeOrVoidString(xmlSistemaMovimenti.ATTR_n_multiple_sis);
textView.setText(text);
// System Bet Text
textView = (TextView) quotesView.findViewById(R.id.systemBetText);
textView.setText(R.string.importoPerColonna);
// System bet Value
textView = (TextView) quotesView.findViewById(R.id.systemBetValue);
text = "€ " + XmlScommessaInCorso.getQuotaFormattedOrVoid(_systems.get(i).getAttributeOrVoidString(xmlSistemaMovimenti.ATTR_impo_sistema));
textView.setText(text);
LinearLayout primaryLayout = (LinearLayout) findViewById(R.id.primaryLayout);
primaryLayout.addView(quotesView);
}
}
}

Categories

Resources