How to add subview of some view in xml layout - android

I am a beginner programming android. I am searching for info how to create transparent sub view on current window layout.
I have created simple layout this is source:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple text" />
</LinearLayout>
Now i want to create a View when navigation button is pressed. That View i want to add on top of this layout with transparent about 40%. It should look something like this:
Also it should be easy to add Buttons, Drop box or else and i could easy remove this View.
Maybe someone did this and could me share ideas, how do do this ?
Thanks.

check out this and do programatically
view.getBackground().setAlpha(100); // to make background transparent
you can use PopupWindow to show on top of parent view as following
View mainview ;
PopupWindow popupwindow;
public void onCreate(Bundle savedInstanceState){
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainview = inflater.inflate(R.layout.main, null, false);
setContentView(mainview);
// load sub menu from xml layout in popupwindow
View submenu_popup = inflater.inflate(R.layout.submenu_popup, null, false);
// make backgraound transparent of popup submenu
submenu_popup.getBackground().setAlpha(100);
popupwindow = new PopupWindow(submenu_popup ,300,300,false);
popupwindow.setOutsideTouchable(true);
popupwindow.setTouchable(true);
}
// call it on click of button or menu to show submenu
public void onClickButton(){
int x=0,y=0;
// show popupwindow on x, y position in main view (parent view) by using this
popupwindow.showAtLocation(mainview , Gravity.NO_GRAVITY, x, y);
}

May be you should learn how to use "android:theme" property

Related

why dynamically set text to TextView not visible in popup window while hard coded is

I am trying to generate popup window containing dynamically generated text. log output clearly shows that the desired text is actually set to textView but I can't understand why it is not showing on the app.
MainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
linearLayout = (LinearLayout) findViewById(R.id.main_activity_parent_layout);
int id = item.getItemId();
if(id == R.id.info_button){
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
//Updating a textview in different layout xml from the main activity
LinearLayout vi = (LinearLayout)layoutInflater.inflate(R.layout.activity_info, null);
TextView textView = vi.findViewById(R.id.info);
textView.setText(R.string.main_activity_info);
Log.i("MainActivity",String.valueOf(textView.getText()));
ViewGroup container = (ViewGroup)layoutInflater.inflate(R.layout.activity_info,null);
popupWindow = new PopupWindow(container, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//Note showAtLocation method should always in end for proper rendering of poup Window
popupWindow.showAtLocation(linearLayout,Gravity.TOP,0,100);
}
return super.onOptionsItemSelected(item);
}
activity_info.xml
<?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:layout_height="match_parent">
<TextView
android:id="#+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="8dp"
android:text="hello"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/black"/>
</LinearLayout>
popup window show hello which is hardcoded text.
You are inflating R.layout.activity_info two times and losing the TextView you are changing in the process. Make the following change to keep the first inflation and your modification:
popupWindow = new PopupWindow(vi, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);

Is it possible to customise Android PopupMenu?

Is it possible to customise PopupMenu layout ? I just need with rounded corners, is it possible to customise with the stock one ?
For custom layouts don't use PopupMenu, alternate option is a PopupWindow
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay() {
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_popupwindow_layout, null);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
And create this my_popupwindow_layout.xml file in the res/layout folder, which is used to inflate the custom layout for the popupWindow.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>

Android Spinner Toolbar style

How do I achieve the material design of spinner in toolbox? I tried the following but my spinner looks like this
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/main_color"
app:contentInsetLeft="14dp"
app:contentInsetRight="14dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<Spinner
android:id="#+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
My minimum sdk is 15.
on my onCreate
ButterKnife.inject(this);
mToolbar.inflateMenu(R.menu.menu_login);
mToolbar.setTitle(R.string.sign_in);
mToolbar.setOnMenuItemClickListener(this);
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.options, android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(spinnerAdapter);
mSpinner.setOnItemSelectedListener(this);
A menu is a temporary sheet of paper that always overlaps the App Bar,
rather than behaving as an extension of the App Bar.
any links would be a great help
UPDATE
I have managed to achieve what I want
I used this
ButterKnife.inject(this);
mToolbar.setTitle(R.string.sign_in);
setSupportActionBar(mToolbar);
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getSupportActionBar().getThemedContext(),
R.array.options, android.R.layout.simple_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(spinnerAdapter, this);
and my xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/app_secondary_color"
app:contentInsetLeft="14dp"
app:contentInsetRight="14dp"
app:popupTheme="#style/ThemeOverlay.AppCompat"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
But How do I make It white and the text color of the menu is blue?
also I have this problem. it seems it is too far away? any way to fix this dropdown arrow?
Update 2
Well after spending so much time trying, I have achieve how to make it white and the text color black
through this. I have created my custom layout for dropdown style
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:background="#color/background_material_light"
android:textColor="#color/abc_primary_text_material_light"
android:layout_height="48dp"
android:ellipsize="marquee"/>
then added this code on my oncreate
spinnerAdapter.setDropDownViewResource(R.layout.custom_simple_spinner_dropdown_item);
The Only Problem now I encounter is that the press state is not working, it is just a plane white. if you see on the other pop up menu, when you click an item the background color becomes a little bit darker. Also this annoying icon is still too far from the text.
If u want press state to work, you should add a Drawable selector instead of color as a background.
Selector example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector>
You should make a color state list resource and apply it as background for your CheckedTextView. As for arrow icon being too far, AFAIK it's because of the length of the children, it goes as far as the longest child. So, no way to customize that.
try this code for change first item font color or background color for Spinner
Spinner spinner = findViewById(R.id.spn);
ArrayList<String> lst = new ArrayList<>();
lst.add("Android");
lst.add("Iphone");
lst.add("Windows");
ArrayAdapter<String> adapter = new ArrayAdapter<String >(getApplicationContext(),R.layout.spin_item,lst)
{
public View getDropDownView(int position, #Nullable View convertView, #NonNull ViewGroup parent)
{
View v = null;
if (position == 0) {
TextView tv = new TextView(getContext());
tv.setTextColor(Color.parseColor("#007f00"));
v = tv;
}
else {
v = super.getDropDownView(position, null, parent);
}
return v;
}
};
spinner.setAdapter(adapter);
We can use PopupWindow to display views/spinner over given view using showAsDropDown().
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
#SuppressLint("InflateParams")
View popupView = layoutInflater.inflate(R.layout.menu_logout, null);
PopupWindow popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.showAsDropDown(logoutParent);
TextView textView = popupView.findViewById(R.id.menu);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
doLogoutWork();
}
});

how to add another view using layout inflatter

I created mainLayout with two buttons
add: to add other layout
remove : to remove other layout.
<Button
android:id="#+id/btnAdd"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Add View"
android:onClick="addView" />
<Button
android:id="#+id/btnRemove"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Remove View"
android:onClick="removeView" />
now i wrote following code to add the view
when I click on addView button
LayoutInflater inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.other_layout,null);
mainLayout.addView(view);
the view is added below the main layout.
But I want the view to add in middle of the screen not at the bottom of screen.
How can I do that?
Modify this line and put ur parent view.
view=inflater.inflate(R.layout.other_layout,PARENT_VIEW_OBJECT);
It should work
get the parentLayout like this
parentLayout=(YourParentLayout)view.findViewById(R.id.parentLayout);
then
parentLayout.addView(yourview);
You can check below code, its working fine for me
private View view = null ;
Button addbutton = null;
ViewGroup parent = null;
LayoutInflater inflater = null;
inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
addbutton = (Button)findViewById(R.id.btnAdd);
parent = (ViewGroup) findViewById(R.id.mainxml);
addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view = inflater.inflate(R.layout.other, null);
parent.addView(view);
}
});
I would put an empty LinearLayout placeholder at the top of your main layout where you want your view to be and add the view to that instead of the main layout.
You can adjust the placeholder first and modify it's location and looks.
<LinearLayout
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
..../>
<Button
..../>
I would look into using a ViewStub it holds a place until .inflate() is called. That way the layout isn't taking resources until it's needed.

Android - Dynamically Add Views into View

I have a layout for a view -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0px"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/items_header"
style="#style/Home.ListHeader" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/items_none"
android:visibility="gone"
style="#style/TextBlock"
android:paddingLeft="6px" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/items_list" />
</LinearLayout>
What I want to do, is in my main activity with a layout like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0px"
android:id="#+id/item_wrapper">
</LinearLayout>
I want to loop through my data model and inject multiple views consisting of the first layout into the main layout. I know I can do this by building the controls completely within the code, but I was wondering if there was a way to dynamically build the views so that I can continue using a layout instead of putting everything in code.
Use the LayoutInflater to create a view based on your layout template, and then inject it into the view where you need it.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
You may have to adjust the index where you want to insert the view.
Additionally, set the LayoutParams according to how you would like it to fit in the parent view. e.g. with FILL_PARENT, or MATCH_PARENT, etc.
See the LayoutInflater class.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup parent = (ViewGroup)findViewById(R.id.where_you_want_to_insert);
inflater.inflate(R.layout.the_child_view, parent);
It looks like what you really want a ListView with a custom adapter to inflate the specified layout. Using an ArrayAdapter and the method notifyDataSetChanged() you have full control of the Views generation and rendering.
Take a look at these tutorials
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
http://developerlife.com/tutorials/?p=327
http://www.androidguys.com/2008/07/14/fancy-listviews-part-one/
To make #Mark Fisher's answer more clear, the inserted view being inflated should be a xml file under layout folder but without a layout (ViewGroup) like LinearLayout etc. inside. My example:
res/layout/my_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/i_am_id"
android:text="my name"
android:textSize="17sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
Then, the insertion point should be a layout like LinearLayout:
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/aaa"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/insert_point"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</RelativeLayout>
Then the code should be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.my_view, null);
ViewGroup main = (ViewGroup) findViewById(R.id.insert_point);
main.addView(view, 0);
}
The reason I post this very similar answer is that when I tried to implement Mark's solution, I got stuck on what xml file should I use for insert_point and the child view. I used layout in the child view firstly and it was totally not working, which took me several hours to figure out. So hope my exploration can save others' time.
// Parent layout
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.layout);
// Layout inflater
LayoutInflater layoutInflater = getLayoutInflater();
View view;
for (int i = 1; i < 101; i++){
// Add the text layout to the parent layout
view = layoutInflater.inflate(R.layout.text_layout, parentLayout, false);
// In order to get the view we have to use the new view with text_layout in it
TextView textView = (TextView)view.findViewById(R.id.text);
textView.setText("Row " + i);
// Add the text view to the parent layout
parentLayout.addView(textView);
}

Categories

Resources