Android: Programmatically set checkbox set text position on the right - java

How can I set a programmatically created checkbox's text to be aligned on the left instead of right side of the checkbox. Below is a code snippet:
Checkbox check1 = new Checkbox(getApplicationContext());
check1.setLayoutParams(new ActionBar.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
check1.setId(fieldNo);
check1.setTextColor(Color.BLACK);
check1.setGravity(Gravity.RIGHT);
check1.setText(formField.get(fieldNo));
The above code resulted in the text shown on the right of the checkbox.
Here is a screenshot :
How can I have the text on the left of the checkbox?

You need to change the following
check1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
In this line you were using ActionBar params and then LinearLayoutCompat params. Try to stick to 1 category and in custom views like checkbox just LinearLayout would do.
UPDATE 1:
You should use CheckedTextView. I have used standard android drawable for that but you can also use your custom Check box design as well.
So your overall code would look like -
final CheckedTextView check1 = new CheckedTextView(getApplicationContext());
check1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
check1.setId(fieldNo);
check1.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
check1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (check1.isChecked()){
check1.setChecked(false);
check1.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
}else{
check1.setChecked(true);
check1.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
}
}
});
check1.setTextColor(Color.BLACK);
check1.setGravity(Gravity.LEFT);
check1.setText(formField.get(fieldNo));

Related

How to refresh layout/context from list adapter (I think this is what i need)

Im having a bug that i cant understand the reason and how to resolve it. I belive that is a problem of layout/view/context refresh but i dont know.
I have a cell from a listView(I prefer recyclerView but the project has years) and in the corner of the cell i have a button to show more options. Programatically it just make an element View.GONE and another element View.VISIBLE.
I will attach code in a moment
To this button i setted too a listener that when i tap on it it do the opposite of below mentioned. It shows some elements and hide an entire LinearLayout from the cell. The elements are showed BUT the LinearLayout keeps on the screen like bugged. If i tap anywhere it disappears and if i try to tap on it it disappears too. Its like the view of that linear got bugged and keep in there like a ghost view. I will shop some pictures.
The cell normally at the beginning: https://imgur.com/1BjK0KP
The cell after i press the entire view to show the LinearLayout at the bottom of the cell: https://imgur.com/eONSptW
The cell after i press the arrow of the corner to hide the LinearLayout. Here it shows the view bugged https://imgur.com/jrT0qxV
The cell after i tap anywhere else https://imgur.com/XD1jN7U
public void expandView(View view){
final View cellView = view;
final LinearLayout editLinear = (LinearLayout) view.findViewById(R.id.cart_edit);
editLinear.setVisibility(View.VISIBLE);
final TextViewFont countText = (TextViewFont) view.findViewById(R.id.itemCount);
countText.setVisibility(View.GONE);
final TextViewFont total = (TextViewFont) view.findViewById(R.id.itemTotal);
final ImageView imageViewArrow = (ImageView) view.findViewById(R.id.cart_edit_image);
imageViewArrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arrow_up));
//notifyDataSetChanged();
imageViewArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
countText.setVisibility(View.VISIBLE);
editLinear.setVisibility(View.GONE);
imageViewArrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arrow_down));
}
});
First of all, you don't need to store context because in View you have the method view.getContext().
I recommend this article:
https://possiblemobile.com/2013/06/context/
On the other hand, ensure that you don't have a ghost view that is overlapping your image view.

Android add a picture in the top right of a button programmatically

I have a button and I want to pro grammatically add a picture of a green tick to the top right button after it has been selected, how would one do that?
First call the relative layout family and using LayoutPram update the position of the picture
ImageView imageView = (ImageView)findViewById(R.id.theImage);
Button myBtn = (Button)findViewById(R.id.myButton);
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageView.setLayoutParams(params);
}
});
Try this!
Use ImageButton and onClick swap out the image
Its a vague answer but your question is also vague

Empty ListView show another activity

I am making an application which uses ListView to show DB elements. But in the first opening or when user will delete everything DB will be empty and then ListView will show blank screen. I want to show a message when it happens. Not only TextView but also Button. For example "DB now empty. Click on button and try to add some records". ListView 's setEmptyView method allows to add only TextView . Is it really possible to create a layout and show it when ListView is empty?
You can use the property setEmptyView.
If the list is empty and your desired display.
ListView listView = (ListView) findViewById(R.id.lst);
View child = getLayoutInflater().inflate(R.layout.empty_view, null);
((ViewGroup)listView.getParent()).addView(child);
listView.setEmptyView(child);
You can dynamically add a button in the activity layout if your list is empty.
Set onClickListner on the button and override the onClick().
Refer below code:
LinearLayout linear = (LinearLayout)findViewById(R.id.layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId("btnDynamic");
btn.setText("Go to create entry page");
linear.addView(btn, params);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
Let me know if this helps.
You can pre-define a layout containing text view and and button as you stated in your question and set it's visibility = gone in your xml or in java code. When the List View will be empty you can set visibility the visibility of the listview.setVisibility(View.GONE) and set your's layout (containing button and a textview) layout.setVisibility(View.VISIBLE)

How I can implement a ExpandableListView in java (for Android) that make click display items?

I'm trying to implement a ExpandableListView in java that when click deployment space elements inside, not the typical wish list allows choosing an option. What I try to do is basically something like this
One can see that the spinner "wraps" to have items and does not have a list, Would I could help a little?
You can use ExpandableListView .. check this tutorial http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
You can easily create one on your own. Just create a Button and assign a Layout below. When the Button gets clicked set the View of you Layout to Visible and otherwise to Invisible. Additionaly you can set up and down arrows to the right of your Button.
final Button button = (Button) findViewById(R.id.button);
final LinearLayout linearLayout= (LinearLayout) findViewById(R.id.linearLayout);
linearLayout.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (linearLayout.getVisibility() == View.VISIBLE) {
linearLayout.setVisibility(View.GONE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.expand, 0);
} else {
linearLayout.setVisibility(View.VISIBLE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.collapse, 0);
}
}
});

how to make entire view in dialog clickable?

i have dialog and text inside eache view that i can click on .
i want all the view to be clickable insted only the text .
the code :
final Dialog dialog = new Dialog(List_Lists.this);
dialog.setContentView(R.layout.dialog_edit_tables);
dialog.setTitle("Action for " + table);
TextView delete = (TextView) dialog
.findViewById(R.id.tvDeleteTable);
TextView cancel = (TextView) dialog.findViewById(R.id.tvCancel);
OnClickListener l = new OnClickListener() {
#Override
public void onClick(View v) {
dbAdapter = new DBmethods(getApplicationContext());
switch (v.getId()) {
case R.id.tvDeleteTable:
viewListsAdapter.listsV.remove(pos);
dbAdapter.deleteTable(table);
break;
case R.id.tvCancel:
dialog.dismiss();
break;
case R.id.bTableRenameName:
if(dbAdapter.checkTableNameOK(List_Lists.this ,newName.getText().toString())){
viewListsAdapter.listsV.remove(pos);
viewListsAdapter.listsV.add(pos, newName.getText().toString().trim());
dbAdapter.renameTable(table , newName.getText().toString().trim());
renamedialog.dismiss();
};
break;
default:
break;
}
// if button is clicked, close the custom dialog
dialog.dismiss();
lv.invalidateViews();
}
};
delete.setOnClickListener(l);
cancel.setOnClickListener(l);
dialog.show();
}
});
}
as i said this code work fine if user press on text inside the view , but the empty view obviously wont respond . thanks !
if user press on red spot it activate the enter , if green than duplicate and so on...the current status is u have to press on the word , if u press on the colored spots it will do nothing
If im not wrong you need this to make layout clickable
try something like this
You can add a OnClickListener on it :
//onCreate
LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout01);
layout.setOnClickListener(yourOnClickListener);
Should be working ;)
create an id of your dialog_edit_tables
let say android:id=#+id\testing in your xml.
now write this
LinearLayout yourlayout = (LinearLayout) findViewById(R.id.testing);
yourlayout.setOnClickListener(l);
hope this will help.
You haven't shown the XML layout but setting the width of the clickable text view to fill_parent should work, also make sure your parent layout also has the width set to fill_parent.
The onClickListener is not based on the text it is based on the actual component, therefore it looks as if the layout_width attribute is set to wrap_content, so the component is only then length of the text, therefore only where the text is, is clickable. Hope this makes sense.
If I am not wrong than you are using listview or tableview to display the text.
You can do one thing. Make the textview layout_width as fill_parent That way you will able to get the touch on the place that you marked.
Hope this will help.

Categories

Resources