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.
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 couldn't phrase the question perfectly with my problem, but basically what I want to achieve is this:-
I have recyclerview of items that contains textview for example, when the item is clicked; a custom dialog will show containing the same content of the clicked element.
so far so good, in the same dialog there's two arrow (left & right) at each of its sides, the arrows buttons should navigate between the recyclerview items and update the content according to the new position.
Here's what I did:
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.tvTitle.setText(data.get(position).getTitle());
holder.itemView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Initializing the custom dailog
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow() .setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().getAttributes().gravity = Gravity.TOP;
dialog.setContentView(R.layout.dailog);
dialog.show();
ImageView RightArrow = (ImageView) dialog.findViewById(R.id.dia_right_arrow);
ImageView LeftArrow = (ImageView) dialog.findViewById(R.id.dia_left_arrow);
final TextView textViewTitle = (TextView) dialog.findViewById(R.id.dia_tvTtile);
textViewTitle.setText(data.get(position).getTitle());
RightArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int curPosi = 0;
if (data.size() != 0) {
curPosi = position+1;
}
textViewTitle.setText(data.get(position).getTitle());
}
});
LeftArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int curPosi = 0;
if (data.size() != 0) {
curPosi = position-1;
}
textViewTitle.setText(data.get(position).getTitle());
} });
}});
}
The Result:
When I click on the right arrow it works 100% , but if I click again nothing happens !!
And If click the left arrow at anytime it gives crash with this exception:
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
Is it possible to achieve a loop effect, what I mean is when I reach
the last index and the click again it shows the first index and vice
versa..!
Any help?
It seems the issue is from how you check your click limits. You can try this
Add curPosi as a class member variable
private int curPosi;
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.tvTitle.setText(data.get(position).getTitle());
holder.itemView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
curPosi = position
//Initializing the custom dailog
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow() .setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().getAttributes().gravity = Gravity.TOP;
dialog.setContentView(R.layout.dailog);
dialog.show();
ImageView RightArrow = (ImageView) dialog.findViewById(R.id.dia_right_arrow);
ImageView LeftArrow = (ImageView) dialog.findViewById(R.id.dia_left_arrow);
final TextView textViewTitle = (TextView) dialog.findViewById(R.id.dia_tvTtile);
textViewTitle.setText(data.get(position).getTitle());
RightArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int lastIndex = data.size()-1;
if (lastIndex == curPosi ) { // this prevents the crash, so if the next button is clicked while the last index is showing it will reset and show the first one..!
curPosi = 0;
} else if(curPosi < data.size()) {
curPosi++;
}
textViewTitle.setText(data.get(curPosi).getTitle());
}
});
LeftArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//same thing here with this "if", when the previous button clicked from the first; it will show the last item..!
if (curPosi == 0) {
curPosi = data.size();
}
if (curPosi > 0) {
curPosi--;
}
textViewTitle.setText(data.get(curPosi).getTitle());
} });
}});
}
I'm searching for a code that it doesn't need a long .xml and I can easily change number of buttons too 200 or everything.
public class buttons extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buttons);
}
}
in activity file add this:
// create buttons in a loop
for (int i = 0; i < 200; i++) {
Button button = new Button(this);
button.setText("Button " + i);
}
Something like that
public class Buttons extends Activity {
{
Button button;
List<Button> buttonList = new ArrayList<Button>();
LinearLayout.LayoutParams params;
LinearLayout list;
OnClickListener listener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.long);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
list = (LinearLayout) findViewById(R.id.list);
listener = new OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
Button b = buttonList.get(id);
///
}
};
for (int i = 0; i < 200; i++)
addButton();
}
public void addButton()
{
button = new CheckBox(this);
button.setLayoutParams(params);
button.setText("TEXT");
button.setId(buttonList.size());
button.setOnClickListener(listener);
buttonList.add(button);
list.addView(button);
}
}
Hi,
I have an issue as the above image shown, on the left there is a button inside a webview. When I click on button a popupwindow in android should be appeared. In that popup window i need a webview.
now its all working fine without the webview in popup window.
this popup window is invoked by javascript interface
public class AppJavaScriptProxy {
private Activity activity = null;
public AppJavaScriptProxy() {
}
#JavascriptInterface
public String showMessage(String footNoteNo) {
Integer footNoteNoInt = Integer.parseInt(footNoteNo);
footnote = myDbHelper.getFootnote(chapterNumber, footNoteNoInt);
try {
LayoutInflater inflater = (LayoutInflater) HomeActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.footnote_popup,
(ViewGroup) findViewById(R.id.popup_footnote));
popup = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
txt_footnote=(TextView) layout.findViewById(R.id.text_footnote);
txt_footnote.setTypeface(malayalamfont);
popup_close = (ImageButton) layout.findViewById(R.id.btn_close_popup);
txt_footnote.setText(footnote);
final WebView footnoteWView = (WebView) layout.findViewById(R.id.footnotePopupWebview);
footnoteWView.getSettings().setJavaScriptEnabled(true);
footnoteWView.loadUrl("file:///android_asset/www/footNotePopup.html");
footnoteWView.clearCache(true);
footnoteWView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){ footnoteWView.loadUrl("javascript:getFootnote('" + footnote + "')");
}
});
popup_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
catch (Exception e) {
e.printStackTrace();
};
}
}
please help
Try this.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title here");
WebView wv = new WebView(this);
wv.loadUrl("http:\\www.yourweb.com");
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert.show();
public class List extends ActionBarActivity implements OnClickListener {
LinearLayout layoutOfPopup;
PopupWindow popupMessage;
Button popupButton;
WebView popupText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
init();
popupInit();
public void init() {
popupButton = (Button) findViewById(R.id.textview1);
popupText = new WebView(this);
popupText.setBackgroundColor(Color.TRANSPARENT);
String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
String summary = "Some text go here</body></html>";
popupText.loadData(String.format(text, summary), "text/html", "utf-8");
popupText.setVerticalScrollBarEnabled(true);
popupText.setHorizontalScrollBarEnabled(true);
insidePopupButton = new Button(this);
insidePopupButton.setText("(X)Close");
insidePopupButton.setBackgroundResource(R.drawable.myborder);
layoutOfPopup = new LinearLayout(this);
LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutOfPopup.addView(insidePopupButton, lpView);
layoutOfPopup.addView(popupText);
layoutOfPopup.setOrientation(1);
layoutOfPopup.setBackgroundColor(Color.WHITE);
}
public void popupInit() {
popupButton.setOnClickListener(this);
insidePopupButton.setOnClickListener(this);
popupMessage = new PopupWindow(layoutOfPopup,
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
popupMessage.setContentView(layoutOfPopup);
}
#Override
public void onClick(View v) {
//Code goes here and to open the show as drop down
and dismiss
}
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...