I want use Spinner in Custom Dialog Box. i use activity show as Dialog Box, inside activity use Spinner. but when convert Activity to Dialog Box Spinner's items not shown !!
My XML Code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/add_sms_dialog_background"
tools:context="com.tellfa.smsbox.addSMS_Page">
<RelativeLayout
android:id="#+id/addSMS_cover_layout"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#drawable/add_sms_dialog_coverlayout">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/addSMS_profile_image"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:src="#drawable/all_users_avatar"
app:border_color="#color/activity_bg_color"
app:border_width="2dp" />
<com.tellfa.smsbox.components.tellfa_TextView
android:id="#+id/addSMS_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="#+id/addSMS_profile_image"
android:text="title"
android:textColor="#color/activity_bg_color"
android:textSize="23sp" />
<ImageView
android:id="#+id/addSMS_close_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:alpha="0.7"
android:src="#drawable/close_dialog" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/addSMS_smsInfo_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="#+id/addSMS_cover_layout"
android:layout_marginBottom="200dp">
<com.rey.material.widget.Spinner
style="#style/Material.Widget.Spinner.Light"
android:id="#+id/spinner_no_arrow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="128dp"
android:layout_marginTop="16dp"
app:rd_style="#style/Material.Drawable.Ripple.Wave.Light"
app:rd_enable="true"
app:spn_labelEnable="true"
app:spn_label="Spinner without arrow"
app:spn_arrowSize="0dp"
app:spn_popupItemAnimation="#anim/abc_fade_in"/>
</RelativeLayout>
</RelativeLayout>
my Java code :
final Dialog dialog = new Dialog(Main_Page.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.add_sms);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Window window = dialog.getWindow();
window.setLayout(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
dialog.setCancelable(false);
ImageView closeDialog_image = (ImageView) dialog.findViewById(R.id.addSMS_close_image);
closeDialog_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
});
i use this library for Spinner : Click to see
how to fix it?
Provide an adapter for spinner , without it no item will be shown provide this code in your dialog
String[] items ={"item 1","item 2","item 3"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin=(Spinner)dialog.findViewById(R.id.spinner_no_arrow);
spin.setAdapter(adapter);
Related
//Here is my fragment
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<RelativeLayout
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="#dimen/_420sdp"
android:background="#EAEAEA"
>
<RelativeLayout
android:id="#+id/home_bg_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/_400sdp"
android:background="#drawable/sama">
<Button
android:id="#+id/bookAppointmentBtn"
android:layout_width="match_parent"
android:layout_height="#dimen/_55sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginRight="#dimen/_30sdp"
android:fontFamily="#string/roboto_light"
android:textSize="#dimen/_20sdp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/_10sdp"
android:background="#drawable/book_appointmentbg"
android:textColor="#color/white"
android:text="#string/bookappointment" />
</RelativeLayout>
</RelativeLayout>
<Button
android:id="#+id/ContactNoBtn"
android:layout_width="match_parent"
android:layout_height="#dimen/_46sdp"
android:text="#string/shop_number"
android:textColor="#ffffff"
android:textSize="#dimen/_25sdp"
android:layout_below="#+id/discountsTv"
android:textAllCaps="false"
android:layout_marginRight="#dimen/_30sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:background="#drawable/black_background"/>
<LinearLayout
android:id="#+id/servicesLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_marginLeft="#dimen/_20sdp"
android:layout_marginRight="#dimen/_20sdp"
android:orientation="horizontal"
android:layout_below="#+id/servicesupperview"
>
<LinearLayout
android:id="#+id/menLayout"
android:layout_width="#dimen/_108sdp"
android:layout_height="#dimen/_27sdp"
android:layout_margin="#dimen/_10sdp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#string/roboto_medium"
android:textColor="#535353"
android:layout_marginStart="#dimen/_17sdp"
android:layout_marginTop="#dimen/_5sdp"
android:textSize="#dimen/_12sdp"
android:text="#string/mens"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/womenLayout"
android:layout_width="#dimen/_108sdp"
android:layout_height="#dimen/_27sdp"
android:layout_margin="#dimen/_10sdp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#string/roboto_medium"
android:textSize="#dimen/_12sdp"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"
android:textColor="#535353"
android:text="#string/womens"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/childrenLayout"
android:layout_width="#dimen/_108sdp"
android:layout_height="#dimen/_27sdp"
android:layout_margin="#dimen/_10sdp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/_12sdp"
android:fontFamily="#string/roboto_medium"
android:textColor="#535353"
android:layout_marginStart="#dimen/_20sdp"
android:layout_marginTop="#dimen/_5sdp"
android:text="#string/children_caps"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I am having toolbar in MainActivity. This is my fragment i want to set title, image and back button inside the toolbar. Here i am using bottom navigation to change the fragments. Each and every fragment i want to set different title and image. I have to set title as center of the toolbar and back button as right corner of the toolbar.
In your MainActivity add these lines in OnCreate() method
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("My Activity Title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
To use Back arrow you can write this code outside onCreate()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Toast.makeText(this, "Back button pressed", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
In you fragment you can change title of Toolbar
getActivity().setTitle("My fragment Title");
You can Create custom Toolbar with ImageView and TextView with your arrow .
include_tool_bar.xml
<com.mastercard.wallet.ui.widget.CustomTextView
android:id="#+id/toolbar_title"
style="#style/Toolbar.TitleText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|left"
android:textSize="#dimen/px_48"
app:fontName="InterStateLight"
customviews:fontName="InterStateLight" />
<ImageView
android:id="#+id/toolbar_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#string/adaCitiPayLogoText"
android:src="#drawable/ab_logo"
android:visibility="gone" />
And Add Toolbar
Toolbar toolbar = (Toolbar) appCompatActivity.findViewById(R.id.toolbar);
ImageView logo = (ImageView) toolbar.findViewById(R.id.toolbar_logo);
CustomTextView textView = (CustomTextView) toolbar.findViewById(R.id.toolbar_title);
logo.setVisibility(View.VISIBLE);
logo.setImageResource(logoResourceId);
textView.setVisibility(View.GONE);
appCompatActivity.setSupportActionBar(toolbar);
appCompatActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);
title of the toolbar can be changed from fragment calling this in onActivityCreated
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((MainActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.menu_about_us));
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
}
in your activity make sure to add a toolbar like this.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
and set it in your activity
setSupportActionBar(findViewById(R.id.toolbar));
and for the back press just add a listener and do the needful.
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// pop up fragment or take another action
}
});
I added an Action button to show about Alert Dialog for my app that I'm developing.... I just fetched one xml file for that Action Dialog popup.
I used this xml as Alert Dialog popup. using below java code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="300dp"
android:weightSum="1">
<TextView
android:id="#+id/info"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:text="Click the links below to contact us"
android:layout_marginTop="13dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/wa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/wa"
android:textSize="30dp"
android:layout_marginTop="8dp"
android:layout_below="#+id/info"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fb"
android:textSize="30dp"
android:layout_marginTop="8dp"
android:layout_below="#+id/wa"
android:layout_centerHorizontal="true" />
</RelativeLayout>
java code :
public void info(MenuItem item) {
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.alertdiag, null);
TextView waf = (TextView) findViewById(R.id.wa);
TextView fbf = (TextView) findViewById(R.id.fb);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("About");
builder.setView(v);
builder.setCancelable(false);
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Now I can see those TextViews in my main activity as alert Dialog. But I can't click the links. Any solution????
Thank you in advance.....
Use AlertDialog.setView() to display a custom layout containing your hyperlink TextView in the message area.
E.g. Make a hyperlink_layout.xml containing a TextView with id = hyperlink_text
I've encountered a lot of pop up images like the example below, i've made a pop up image but there is no close button at the top left corner, do u guys have any idea how to make a pop up image with close button at the top left cornel like the example below in Android?
You need to make your own Dialog with your custom layout
Dialog dialog;
private void showDialog() {
// custom dialog
dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
// set the custom dialog components - text, image and button
ImageButton close = (ImageButton) dialog.findViewById(R.id.btnClose);
Button buy = (Button) dialog.findViewById(R.id.btnBuy);
// Close Button
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
//TODO Close button action
}
});
// Buy Button
buy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
//TODO Buy button action
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#android:color/white"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/images" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="#+id/btnBuy"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/holo_green_light"
android:text="BUY"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/btnBuy"
android:text="Thank You (Domestic Album Version)"
android:textColor="#android:color/black"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle"
android:text="Still Not Gettin' Any, 2004" />
</RelativeLayout>
</LinearLayout>
<ImageButton
android:id="#+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/black"
android:src="#android:drawable/ic_menu_close_clear_cancel" />
</RelativeLayout>
Result is:
You can use this library. There is close button on top right corner. only need to set image only.
https://github.com/chathuralakmal/AndroidImagePopup
I have created custom Dialog layout. When user press on the button the dialog is shown. Android version lollipop shows me the dialog perfectly but in case of lower version than lollipop it throws me error.
Here is my code:
public class MainActivity extends AppCompatActivity {
private Button click;
private Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click=(Button)findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(click))
{
dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.check_in_weight_dialog);
dialog.create();
dialog.show();
}
}
});
}
And here is my custom dialog layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:padding="16dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check In Weight"
android:textColor="#android:color/black"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="20">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:inputType="number"
android:hint="Current Weight"
android:background="#drawable/editext_border"
android:id="#+id/edtCheckInWhgt"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_weight="10">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:gravity="center"
android:id="#+id/btncheckin"
android:textColor="#android:color/holo_blue_light"
android:background="?android:attr/selectableItemBackground"/>
</LinearLayout>
</LinearLayout>
dialog.create();
this is for lolipop only. Remove this line
Try this way ,It works fine (Lolipop)
custom_dialog = new Dialog(this,android.R.style.Theme_Holo_Light_Dialog_MinWidth);
custom_dialog.getWindow().setBackgroundDrawable(new ColorDrawable((0xff000000)));
custom_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
custom_dialog.setCancelable(false);
custom_dialog.setContentView(R.layout.check_in_weight_dialog);
custom_dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, Color.parseColor("#FFFFFF"));
custom_dialog.show();
Or Remove dialog.create();
dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.check_in_weight_dialog);
dialog.show();
So I have an application that dynamically adds a table row every time the "Add Row" button is pressed. The code attached is the code that runs when "Add Row" is pressed. What I am trying to do is have 3 EditTexts next to each other on the same row. In order to do that, I need to change the Layout_Width of each EditText so the first EditText doesn't cut the other two off the screen. I can't seem to figure out a way to properly do this and was wondering if anyone would help me out. After I figure out how to do that, the next step is to adjust the layout_width according to the screen size but thats later down the road. It needs to be done programmatically because a user can theoretically have as many rows as they want to.
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
tablerow = new TableRow(getActivity());
ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed2 = new EditText(getActivity());
ed2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
ed3 = new EditText(getActivity());
ed3.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
tablerow.addView(ed1);
tablerow.addView(ed2);
tablerow.addView(ed3);
table1.addView(tablerow);
}
};
I believe what you are looking for is LayoutParams and its "weight" value.
Try:
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f);
ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed1.setLayoutParams(params);
The third value (1.0f) in the LayoutParams constructor is a weight...all EditTexts in that TableRow should end up the same width.
You can try next approach, it is based on xml. You will be able to play around with layout at table_row.xml.
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.a_mn_button_add_row:
onClickButtonAddRow();
break;
}
}
private void onClickButtonAddRow() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.a_mn_table);
tableLayout.addView(View.inflate(this, R.layout.table_row, null));
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/a_mn_button_add_row"
android:onClick="onClick"
android:text="Add row"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="#+id/a_mn_table"
android:stretchColumns="0,1,2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
table_row.xml for use case with 3-1-2 proportions.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:layout_weight="1"
android:inputType="text"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:layout_weight="2"
android:inputType="text"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</TableRow>