Access textview on another layout - java

I have made a popup, which works fine, but I'd like to write to the TextView on it. Using the standard method doesn't work (just crashes it), despite Eclipse finding the TextView's id and not showing any problems. The popup is in another XML layout.
Generates the popup
public PopupWindow pw;
public void popUpShow() {
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pw = new PopupWindow(
inflater.inflate(R.layout.popup, null, false),
400,
600,
true);
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_layout"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AAA"
>
<TextView
android:id="#+id/popupOut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text=""
/>
<Button
android:id="#+id/popupclose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close"
android:onClick="popUpHide" />
</LinearLayout>

Before creating your PopupWindow, keep a reference to the view Inflated
ViewGroup v = (ViewGroup)inflater.inflate(R.layout.popup, null, false);
pw = new PopupWindow(
v,
400,
600,
true);
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
TextView view = (TextView)v.findViewById(R.id.textView1);

If the textView is in a dialog, then the view id should be retrieved by
Dialog dialog = new Dialog(this);
TextView view = (TextView)dialog.findViewById(R.id.textView1);
Try
public void popUpShow() {
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout t = (LinearLayout) inflater.inflate(R.layout.item1, null, false);
PopupWindow pw = new PopupWindow(
t,
400,
600,
true);
pw.showAtLocation(t.findViewById(R.id.main), Gravity.CENTER, 0, 0);
}

Related

Add buttons dynamically to ScrollView android

So I want to add x amount of buttons to a layout in android (where x is usually between 4 and 24), and I can achieve this using the code below, but it doesn't scroll so it cuts off some of the buttons
I am using a fragment, which contains a LinearLayout and BottomNavigation, and one of those navigation options leads to the fragment contained below
At the moment my.xml file looks like:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/units_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
And my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_course_units, container, false);
final LinearLayout unitsBtns = (LinearLayout) view.findViewById(R.id.units_group);
//I actually get this from a volley request but didn't want to include all the code
String[] units = {"1", "2", "3", "4", "5", "6", "7", "8"};
for(int i = 0; i < units.length; i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
params.setMargins(0, 30, 0, 0);
Button b = new Button(getActivity());
b.setLayoutParams(params);
b.setText(units[i]);
b.setId(i);
b.setBackgroundColor(Color.WHITE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
// STUFF HERE
} catch (Exception e) {
e.printStackTrace();
}
}
});
unitsBtns.addView(b);
}
return view;
}
Is there any way to get the buttons to be scrollable? I have tried to put the scrollView around the parent of this too and it still doesn't seem to work.
Note: I am still pretty new to developing native Android apps so please correct me if I'm doing something incorrectly
when you have to use ScrollView you should give android:layout_height="wrap_content" in child view of ScrollView then only you have to scroll your view
You should use user LinearLayout like this
<LinearLayout
android:id="#+id/units_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
</LinearLayout>
here also you have to replace
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Note: when you give match_parent in height attribute, it will limit your view to device screen
hope it will help you.. :)
Here's a working example (horizontal) :
<HorizontalScrollView
android:id="#+id/hsv1"
android:scrollbars="none"
android:background="#0d47a1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/hsvLayout1"
android:background="#0d47a1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
Get ScrollView and Layout:
HorizontalScrollView hsv1 = (HorizontalScrollView) findViewById( R.id.hsv1 );
LinearLayout layout = (LinearLayout) hsv1.findViewById( R.id.hsvLayout1 );
layout.removeAllViews();
Create a button dynamically:
Button myButton = new Button (this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT );
lp.setMargins( 20, 0, 20, 0 );
myButton.setLayoutParams(lp);
myButton.setTextColor( Color.YELLOW );
myButton.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );
myButton.setText( "Hello World !" );
Add button to layout:
layout.addView(myButton);

DialogFragment with no title shrinks dialog width

The goal is to create a dialog that shows a list of files.
The problem is that once I remove the title (by any means including window feature, setting style, using styles.xml...) the dialog window converts to wrap_content constraint.
No property I set in layout influences this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/dialog_files_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="8dp"
android:text="#string/dialog_files_title"
android:textColor="#color/colorTextActive"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/dialog_files_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
</LinearLayout>
my java code
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
#SuppressLint("InflateParams") View view = inflater.inflate(R.layout.dialog_files, null);
paths = new ArrayList<>();
recursiveScan(Environment.getExternalStorageDirectory());
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dialog_files_recycler_view);
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
AdapterFiles adapter = new AdapterFiles(getActivity(), paths);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
return view;
}
The result is this
The issue seems to be when using LinearLayout as a root layout. I switched to RelativeLayout everything fit into place. Maybe this is a bug when using LinearLayout as a root layout in dialogs and removing title?
Dialog dialog = super.onCreateDialog(savedInstanceState);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
if (window != null) {
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}
return dialog;
Try this out.

Android set margin of view programmatically

I wanted to set margin to View programmatically. Here is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llAttendeeList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000">
<ListView
android:id="#+id/attendeelistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#F6CECE" >
</ListView>
</LinearLayout>
And the method when my button onClick to call out the popup Window view:
private void openPopUp() {
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.event_attendee_pop,
null);
llAttendeeList = (LinearLayout) popupView
.findViewById(R.id.llAttendeeList);
attendeeListView = (ListView) popupView.findViewById(R.id.attendeelistview);
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(10, 10, 10, 0);
popupView.setLayoutParams(params);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, 350);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popupWindow.dismiss();
return true;
}
return false;
}
});
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
mAdapter = new ListAdapter(getActivity());
attendeeListView.setAdapter(mAdapter);
}
I tried to set margin to the popupView but no luck. The margin is not there. I wonder which part override it.
Manually set width and height of PopupWindow :
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
popupWindow.setWidth(width-10);
popupWindow.setHeight(height-10);
try to use layout it will work
LinearLayout.LayoutParams params = new LayoutParams
(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);
follow this tutorial http://www.codota.com/android/classes/android.view.ViewGroup.MarginLayoutParams

How I can create small buttons in a LinearLayout dynamically creating a line break when you touch the edge of this?

I am trying to create buttons dynamically in one LinearLayout which is in a ExpandibleListView (then it is an adapter), my situation is that when I create a button they occupy the entire width of the screen that can be only 1, but I like to have several buttons depending on only one line of text in each button
My Situation:
I need:
My xml is the follow:
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
<LinearLayout
android:id="#+id/sub_sub_sub_itemes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
></LinearLayout>
</LinearLayout>
And my code java is:
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final OptionItemPromo entry = (OptionItemPromo) getChild(groupPosition, childPosition);
final String childText = entry.getTitle();
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
txtListChild.setTextColor(Color.parseColor("#ffffff"));
LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.sub_sub_sub_itemes);
ll.removeAllViews();
int _width_so_far = 0;
for(int i = 0; i < entry.getSelection_items().size(); i++){
final LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final Button myButton = new Button(this._context);
myButton.setText(entry.getSelection_items().get(i).getName());
myButton.setTextColor(Color.parseColor("#4d4d4d"));
myButton.setBackgroundColor(Color.parseColor("#dedede"));
myButton.setTextSize(16);
ll.addView(myButton, lp);
}
return convertView;
}
set singleLine to true using setSingleLine() metod so that the button's text don't take more than one line and setEllipsize at the End so that if text of button have multiple line then it places Dot (.) at the end. add these two line with your button...
myButton.setSingleLine(true);
myButton.setEllipsize(TextUtils.TruncateAt.END);

Positioning Custom ProgressDialog Android

I have made a transparent custom progress bar.I want to move the progress bar to a certain location on the screen programatically.But every time the custom progress bar get drawn at the middle of the screen.
public class CustomProgressBar extends Dialog {
ProgressBar mProgressBar;
TextView mTextMessage;
public CustomProgressBar(Context context, int id) {
super(context, R.style.ProgressDialogTheme);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_progree_dialog,
null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);//the progress bar does not get aligned to left top with the defined margin space.
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);//
params.setMargins(50, 50, 0, 0);//
// layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, id);
this.addContentView(view, params);
}
}
And this is how I am using it inside my activity
Dialog mProgressBar = new CustomProgressBar(
MyActivity.this,
otherView.getId());
My progress bar layout file custom_progree_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" >
<ProgressBar
android:id="#+id/id_server_connection_progress_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="#drawable/somedrwablefile" />
<TextView
android:id="#+id/id_server_connection_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/padding_30px"
android:layout_marginTop="#dimen/padding_4px"
android:layout_toRightOf="#id/progress_dialog"
android:text="#string/connectingMessage"
android:textColor="#color/white"
android:textSize="#dimen/padding_40px" />
</RelativeLayout>
Please guide me and let me know where I am going wrong.I have spent two days trying to figure out the problem.
So I solved this problem by making changes to my custom_progress_dialog and aligning the progress bar and the text to the location I want.Also I am not using the custom class any more.I am just inflating the layout at runtime and adding it as a child view inside my activity.
if (mView == null)
{
mView = getLayoutInflater().inflate(R.layout.server_connection_progree_dialog,null);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
MyActivity.this.addContentView(mView, layoutParams);
}
else
if (!mView.isShown())
{
mView.setVisibility(View.VISIBLE);
}

Categories