I want to hide check mark in the check box button in class code not xml file .
cuz i'm using two background if checked true set background .. if false set background "checkbox.png"
public class ListActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
AbsoluteLayout ff = (AbsoluteLayout) this.findViewById(R.id.AbsoluteLayout1);
ScrollView myScrollView = (ScrollView) findViewById(R.id.scrollView1);
TableLayout tl =(TableLayout) findViewById(R.id.h103);
TableRow tr = (TableRow) findViewById(R.id.TableRow18);
CheckBox cb = (CheckBox) findViewById(R.id.CheckBox18);
TextView tv = (TextView) findViewById(R.id.TextView34);
// ScrollView myScrollView1 = new ScrollView(this);
TableLayout tl1 =new TableLayout(this);
TableRow tr1 = new TableRow(this);
final CheckBox buttonView = new CheckBox(this) ;
TextView tv1 = new TextView(this);
/*myScrollView.getLayoutParams();
ViewGroup.LayoutParams iv_params_b = myScrollView1.getLayoutParams();
myScrollView1.setLayoutParams(iv_params_b);*/
//buttonView.setVisibility(1);
buttonView.setBackground(getResources().getDrawable(R.drawable.checkbox));
//buttonView.setVisibility(View.GONE);
buttonView.setFocusable(false);
buttonView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox) v).isChecked())
{
//buttonView.setChecked(false);
buttonView.setBackground(getResources().getDrawable(R.drawable.star));
}else
{
//buttonView.setChecked(true);
buttonView.setBackground(getResources().getDrawable(R.drawable.checkbox));
}
}
});
/* if(isChecked)
{
buttonView.setBackground(getResources().getDrawable(R.drawable.star));
}else
{
buttonView.setBackground(getResources().getDrawable(R.drawable.checkbox));
} */
ViewGroup.LayoutParams iv_params_b = tl.getLayoutParams();
tl1.setLayoutParams(iv_params_b);
ViewGroup.LayoutParams iv_params_b1 = tr.getLayoutParams();
tr1.setLayoutParams(iv_params_b1);
ViewGroup.LayoutParams iv_params_b2 = cb.getLayoutParams();
buttonView.setLayoutParams(iv_params_b2);
ViewGroup.LayoutParams iv_params_b3 = tv.getLayoutParams();
tv1.setLayoutParams(iv_params_b3);
tl.addView(tr1);
tr1.addView(buttonView);
}
}
You probably should be using the framework's constants, like View.Gone
save this as a xml in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/checked"/>
<item
android:state_checked="false" android:state_focused="false"
android:drawable="#drawable/notchecked"
/>
</selector>
and set this as your checkbox andriod:button="#drawable/yourxmlfile.
The you dont need to change the image programmatically.
Hope this will help you.
checkbox.setVisibility(View.INVISIBLE) this is the answer for which you have asked.
EDIT
final CheckBox checkBox1=new CheckBox(MainActivity.this);
checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton paramCompoundButton,
boolean paramBoolean) {
// TODO Auto-generated method stub
if (paramBoolean) {
checkBox1.setButtonDrawable(R.drawable.ic_launcher);
//here you can change
}
else
{
checkBox1.setBackgroundColor(Color.BLUE);
}
}
});
check out the code...
Related
My question is fairly simple. I currently have Radiobuttons that look like this :
I would like to set the text to be below the image (and eventually set it as bold when the button is clicked).
My XML file looks like this :
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioGroup
android:id="#+id/radio_group_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
</HorizontalScrollView>
And part of my Java code :
RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);
for(int i=0; i<categories.size(); i++) {
final String name = categories.get(i).name;
final int resource = categories.get(i).resource;
final RadioButton button = new RadioButton(getContext());
button.setButtonDrawable(resource);
button.setText(name);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Set text as bold (I'll add some logic too)
}
});
categoryGroup.addView(button);
}
Try this :
RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);
for(int i=0; i<categories.size(); i++) {
final String name = categories.get(i).name;
final int resource = categories.get(i).resource;
final RadioButton button = new RadioButton(getContext());
Drawable drawable = getResources().getDrawable(resource);
button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
button.setButtonDrawable(null);
button.setText(name);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
button.setText(Html.fromHtml("<h2>"+button.getText().toString()+"</h2>",
Html.FROM_HTML_MODE_COMPACT));
} else {
button.setText(Html.fromHtml("<h2>"+button.getText().toString()+"</h2>"));
}
}
});
categoryGroup.addView(button);
}
Try this
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
RadioGroup radioGroup;
RadioButton radioButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
linearLayout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
radioGroup = new RadioGroup(getApplicationContext());
linearLayout.addView(radioGroup);
for (int i = 0; i<5 ;i++){
radioButton = new RadioButton(getApplicationContext());
radioButton.setText(String.valueOf(i));
radioGroup.addView(radioButton);
}
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.setMargins(3, 0, 3, 5);
getWindow().addContentView(linearLayout, params1);
}
}
This worked for me check this out
this is how it looks.
I have used 9 Buttons and I want to be bigger on anyone clicked, and if any of the exit modes came out, they would return to their default.
All these buttons are used in a grid-layout
I have used 9 Buttons and I want to be bigger on anyone clicked you can see example View in here
this is my source code
public class MainActivity extends AppCompatActivity implements Animation.AnimationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
content = 1;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add category
searchButton = (Button) findViewById(R.id.addCategory);
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final View promptsView = getLayoutInflater().inflate(R.layout.prompts_category, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setView(promptsView);
final AlertDialog dialog = alertDialogBuilder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = 800;
lp.height = 675;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW);
addCategoryButton = (Button) dialog.findViewById(R.id.addCatButton);
insertCategoryName = (EditText) dialog.findViewById(R.id.insertCategoryName);
gridColorsCategory = (GridLayout) dialog.findViewById(R.id.gridColorsCategory);
setSingleEvent(gridColorsCategory);
addCategoryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
insertCategoryName.getText().toString();
}
});
}
});
}
private void setSingleEvent(GridLayout gridColorsCategory) {
for (int i = 0; i < gridColorsCategory.getChildCount(); i++) {
final Button button = (Button) gridColorsCategory.getChildAt(i);
final int finalI = i;
final ViewGroup.LayoutParams layoutParams = button.getLayoutParams();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
});
}
}
you can use this method to resize your button
private void resizeView(View view, int newWidth, int newHeight) {
try {
Constructor<? extends LayoutParams> ctor = view.getLayoutParams().getClass().getDeclaredConstructor(int.class, int.class);
view.setLayoutParams(ctor.newInstance(newWidth, newHeight));
} catch (Exception e) {
e.printStackTrace();
}
}
and if the default is wrap_content
private void resizeDefault(View view) {
try {
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(params);
} catch (Exception e) {
e.printStackTrace();
}
}
You could create an xml file in your drawable folder called something like btn_go.xml that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: order is important (the first matching state(s) is what is rendered) -->
<item
android:state_pressed="true"
android:drawable="#drawable/go_pressed_big” />
<item
android:drawable="#drawable/go_normal_small” />
</selector>
Then, in your view's layout.xml file, you'd reference your button's source like this:
<ImageButton
android:id="#+id/button_go”
android:layout_width=“wrap_contents”
android:layout_height=“wrap_contents”
android:src="#drawable/btn_go”/>
The go_pressed_big image would be slightly larger than the go_normal_small image, and, by specifying wrap_contents for the width and height, the button should adjust.
I'm trying to do something slightly out of the ordinary. I have an Add a Profile page where a user can add multiple profiles. I want to represent these profiles as circles (ImageButtons) on the homepage.
Much like this:
So when a user saves a profile, how could I add one of these circles onto this horizontal scroll view? I would like the created circle to have a letter inside it from the first letter of the created clients name.
I think this would require me to have all 26 letters of the alphabet already created, but this is no problem.
Any suggestions on how to do this? I'm sorry if I'm not clear, I'd be happy to post any more code or clarify anything. The database I use is Firebase. I'm running the latest version of Android Studio.
Any help is greatly appreciated, cheers guys!
AddAProfile.java:
public class AddProfileActivity extends AppCompatActivity {
EditText etName;
EditText etCareer;
CheckBox cbTech;
CheckBox cbMedi;
CheckBox nfRenewableEnergy;
CheckBox nfGoogle;
CheckBox cfNovartis;
CheckBox nfTesla;
CheckBox lrsFB;
CheckBox lrsAAPL;
CheckBox lrsYHOO;
CheckBox cbEURUSD;
CheckBox lrcuUSDRUB;
CheckBox lrcoSILVER;
CheckBox lrcoGOLD;
CheckBox lrcuGBPUSD;
CheckBox lriNSDQ;
CheckBox lriSP500;
Button bSave;
DatabaseReference databaseClients;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
databaseClients = FirebaseDatabase.getInstance().getReference("clients");
etName = (EditText) findViewById(R.id.etName);
etCareer = (EditText) findViewById(R.id.etCareer);
cbTech = (CheckBox) findViewById(R.id.cbTech);
cbMedi = (CheckBox) findViewById(R.id.cbMedi);
nfRenewableEnergy = (CheckBox) findViewById(R.id.nfRenewableEnergy);
nfGoogle = (CheckBox) findViewById(R.id.nfGoogle);
cfNovartis = (CheckBox) findViewById(R.id.cfNovartis);
nfTesla = (CheckBox) findViewById(R.id.nfTesla);
lrsFB = (CheckBox) findViewById(R.id.lrsFB);
lrsAAPL = (CheckBox) findViewById(R.id.lrsAAPL);
lrsYHOO = (CheckBox) findViewById(R.id.lrsYHOO);
cbEURUSD = (CheckBox) findViewById(R.id.cbEURUSD);
lrcuUSDRUB = (CheckBox) findViewById(R.id.lrcuUSDRUB);
lrcoSILVER = (CheckBox) findViewById(R.id.lrcoSILVER);
lrcoGOLD = (CheckBox) findViewById(R.id.lrcoGOLD);
lrcuGBPUSD = (CheckBox) findViewById(R.id.lrcuGBPUSD);
lriNSDQ = (CheckBox) findViewById(R.id.lriNSDQ);
lriSP500 = (CheckBox) findViewById(R.id.lriSP500);
bSave = (Button) findViewById(R.id.bSave);
ImageButton bSettingsBlack = (ImageButton) findViewById(R.id.ibSettingsBlack);
ImageButton bEconCalBlack = (ImageButton) findViewById(R.id.ibEconCalBlack);
ImageButton bLiveRates = (ImageButton) findViewById(R.id.ibLiveRates);
ImageButton bNewsBlack = (ImageButton) findViewById(R.id.ibNewsBlack);
bSettingsBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, SettingsActivity.class));
}
});
bEconCalBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, WebViewActivity.class));
}
});
bLiveRates.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, sortingrates.class));
}
});
bNewsBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, HomePageNews.class));
}
});
bSave.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
bSave();
}
});
}
private void bSave(){
String name = etName.getText().toString().trim();
String career = etCareer.getText().toString().trim();
Boolean techCB = cbTech.isChecked();
Boolean mediCB = cbMedi.isChecked();
Boolean renewableEnergyNF = nfRenewableEnergy.isChecked();
Boolean googleNF = nfGoogle.isChecked();
Boolean novartisNF = cfNovartis.isChecked();
Boolean teslaNF = nfTesla.isChecked();
Boolean fbLRS = lrsFB.isChecked();
Boolean applLRS = lrsAAPL.isChecked();
Boolean yhooLRS = lrsYHOO.isChecked();
Boolean eurusdCB = cbEURUSD.isChecked();
Boolean usdrubCU = lrcuUSDRUB.isChecked();
Boolean silverCO = lrcoSILVER.isChecked();
Boolean goldCO = lrcoGOLD.isChecked();
Boolean gbpusdCU = lrcuGBPUSD.isChecked();
Boolean nsdqI = lriNSDQ.isChecked();
Boolean sp500I = lriSP500.isChecked();
if(!TextUtils.isEmpty(name)){
String id = databaseClients.push().getKey();
Clients clients = new Clients(id, name, career, techCB, mediCB, renewableEnergyNF, googleNF,
novartisNF, teslaNF, fbLRS, applLRS, yhooLRS, eurusdCB, usdrubCU, silverCO,
goldCO, gbpusdCU, nsdqI, sp500I );
databaseClients.child(id).setValue(clients);
Toast.makeText(this, "Client added", Toast.LENGTH_LONG).show();
startActivity(new Intent(AddProfileActivity.this, AddProfileActivity.class));
}else{
Toast.makeText(this, "Please enter your name", Toast.LENGTH_LONG).show();
}
}
}
EDIT (What I've tried):
I tired with other buttons as an experiment to change the image as I couldn't figure out to straight up create it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main17);
ImageView view = (ImageView) findViewById(R.id.imageView3);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.imageView3 == imageView.getId());
Integer integer = (Integer) imageView.getTag();
integer = integer == null ? 0 : integer;
switch(integer) {
case R.drawable.circlesambergrey2:
imageView.setImageResource(R.drawable.bell);
imageView.setTag(R.drawable.bell);
break;
case R.drawable.bell:
default:
imageView.setImageResource(R.drawable.circlesambergrey2);
imageView.setTag(R.drawable.circlesambergrey2);
break;}
}
});
}
I've also thought that the user could select the image button with the correct letter from a spinner. This is what I am currently trying! Cheers.
I need you help.
I made a small application, and it is necessary to bring "tiles" button.
Now i have an array with letters
<string-array name="let_terms">
<item>A</item>
<item>B</item>
<item>C</item>
......
And then programmatically output the button with these letters:
public class letterms extends AppCompatActivity {
String[] mArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.letterms);
int length =0;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
length =getResources().getStringArray(R.array.let_terms).length;
mArray = getResources().getStringArray(R.array.let_terms);
final LinearLayout linearLayout=(LinearLayout)findViewById(R.id.buttonlayout);
for(int i=0;i<length;i++){
final String nazv = mArray[i];
final String[] splittedItem = nazv.split(":");
Button button=new Button(this);
button.setId(i);
button.setWidth(20);
button.setLayoutParams(params);
button.setText(splittedItem[0]);
button.setTextColor(0xFF2C85A6);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent is = new Intent(getApplicationContext(), termin_full.class);
is.putExtra("fVariableName", nazv);
startActivity(is);
}
});
linearLayout.addView(button);
}
}
}
In XML:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonlayout">
</LinearLayout>
All right, go bat buttons in a row, and I would like to carry on a new line when it reaches the end of the screen. How to tile...
Where can I see an example of such an implementation?
Use vertical LinearLayout in XML. Then programmatically create horizontal LinearLayout and add buttons in horizontal layout. For each line, create and add new horizontal layout.
XML:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonlayout">
</LinearLayout>
ACTIVITY:
public class letterms extends AppCompatActivity {
String[] mArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.letterms);
int length =0;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
length =getResources().getStringArray(R.array.let_terms).length;
mArray = getResources().getStringArray(R.array.let_terms);
final LinearLayout verticalLayout= LinearLayout)findViewById(R.id.buttonlayout);
int verticalWidth = verticalLayout.getWidth();
int numberOfButtonsPerLine = (verticalWidth/buttonWidth);
int numberOfLines = (length/numberOfButtonsPerLine) + 1;
for(int i=0;i<length;){
LinearLayout newLine = new LinearLayout(this);
newLine.setLayoutParams(params);
newLine.setOrientation(LinearLayout.HORIZONTAL);
for(int j=0;j<numberOfLines;j++){
final String nazv = mArray[i];
final String[] splittedItem = nazv.split(":");
Button button=new Button(this);
button.setId(i);
button.setWidth(20);
button.setLayoutParams(params);
button.setText(splittedItem[0]);
button.setTextColor(0xFF2C85A6);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent is = new Intent(getApplicationContext(), termin_full.class);
is.putExtra("fVariableName", nazv);
startActivity(is);
}
});
newLine.addView(button);
i++;
if(i>=length) {
break;
}
}
verticalLayout.addView(newLine);
}
}
}
GridLayout is the best option for your scenario. By GridLayout you don't need to calculate the width of the screen.
You can find the guide for it on the link below.
GridView guide
public void responsePost(Editable editable)
{
TableLayout chatbox = (TableLayout)findViewById(R.id.chatbox);
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(this);
textview.setText(editable);
// textview.getTextColors(R.color.)
textview.setTextColor(Color.YELLOW);
tr1.addView(textview);
chatbox.addView(tr1, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
textview.setGravity(Gravity.RIGHT);
}
How can i make it go ALL THE WAY to the right? and not just a little bit? I used setGravity(Gravity.RIGHT) but that did not work.
There is a picture of what it is doing.
I need a little more detail, I couldn't duplicate your layout, what do you have in your main layout, and what code do you have in onCreate for the activity?
All you need is to set columns to stretchable:
chatbox.setColumnStretchable(1, true);
and you need one extra row, leave it blank
TextView col1 = new TextView(this);
col1.setText("");
tr1.addView(col1);
for example the complete code could be:
public class So extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
// anonymous inner class
public void onClick(View view) {
EditText editText1 = (EditText) findViewById(R.id.edit_text);
Editable editable = editText1.getText();
responsePost(editable);
editText1.setText("");
}
});
}
public void responsePost(Editable editable) {
TableLayout chatbox = (TableLayout) findViewById(R.id.chatbox);
TableRow tr1 = new TableRow(this);
// add these lines here
TextView col1 = new TextView(this);
col1.setText("");
tr1.addView(col1);
TextView textview = new TextView(this);
textview.setGravity(Gravity.RIGHT);
textview.setText(editable);
textview.setTextColor(Color.YELLOW);
tr1.addView(textview);
// and this line
chatbox.setColumnStretchable(1, true);
chatbox.addView(tr1, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}