Positioning Custom ProgressDialog Android - java

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);
}

Related

Use stylized action bar in every activity

I wanted to use a custom color and font and size for my app's name in the action bar, so I went in and stylized it in MainActivity.java like so
//stylize the action bar
TextView tv = new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText(R.string.Title);
tv.setTextSize(45);
tv.setTextColor(Color.parseColor("#FFFFFF"));
Typeface tf = Typeface.createFromAsset(getAssets(), "KGALittleSwag.ttf");
tv.setTypeface(tf);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(tv);
updateOptionsMenu();
This did the trick, and then when I added another activity I copied this code into it, and it looked great. At this point my app is almost ready to release, but I'm doing some refactoring and I now have 6 or so activities, and it feels more than a little redundant to put this code in each of them. Is there a better practice for applying these changes to the action bar universally?
This is what inheritance is for.
Create an abstract BaseActivity Class, in which you'll fo all these processing. All your activities where you want to apply these styles will inherit for BaseActivity.
public abstract class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
//stylize the action bar
TextView tv = new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams (ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText(R.string.Title);
tv.setTextSize(45);
tv.setTextColor(Color.parseColor("#FFFFFF"));
Typeface tf = Typeface.createFromAsset(getAssets(), "KGALittleSwag.ttf");
tv.setTypeface(tf);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(tv);
updateOptionsMenu();
}
}
Then your children activities:
public abstract class ChildActivity extends BaseActivity {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState); // Here it calls the parent onCreate method and therefore executes the styling code
}
}
What you can do is just make a XML layout file for your custom action bar, and than use that in all activities.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/actionBarTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="center"
android:maxLines="1"
android:text="your text"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
make sure that your TextView is centered, and that layout_width and layout_hight for your TextView is set to wrap_content
Than put this into every activity that you want to use custom action bar
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);

Using BottomBar prevents fragments from opening?

I am using the Support Library to add a bottom bar similar to the material design one. The bottom bar works great but it seems that if I have the bar displayed, if I try to open any fragment from my custom adapter, the fragment does not open...or maybe it opens behind my main layout? I have no idea how to figure this out. Below is my code.
I've read more posts on SO and around the web and I think this is related to the fragment being properly loaded but below or next to the bottom bar...and that is why it isn't visible? Why does this happen? Is it because the bottom bar has a LinearLayout? I defined it as a menu so I'm not sure if I can control it being a LinearLayout....
Setting up the bottom bar, this method is called from the onCreate of my activity:
public void setupBottomToolbar(Bundle savedInstanceState) {
mBottomBar = BottomBar.attach(MainActivity.this, savedInstanceState);
mBottomBar.setItems(R.menu.bottombar_menu);
mBottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
#Override
public void onMenuTabSelected(#IdRes int menuItemId) {
if (menuItemId == R.id.toolbar_jobs) {
} else if (menuItemId == R.id.toolbar_messages) {
} else if (menuItemId == R.id.toolbar_recentJobs) {
} else if (menuItemId == R.id.toolbar_employerPools) {
}
}
#Override
public void onMenuTabReSelected(#IdRes int menuItemId) {
if (menuItemId == R.id.toolbar_jobs) {
// The user reselected item number one, scroll your content to top.
} else if (menuItemId == R.id.toolbar_messages) {
} else if (menuItemId == R.id.toolbar_employerPools) {
} else if (menuItemId == R.id.toolbar_recentJobs) {
}
}
});
// Setting colors for different tabs when there's more than three of them.
// You can set colors for tabs in three different ways as shown below.
mBottomBar.getBar().setBackgroundColor(getResources().getColor(R.color.laborswipe_darkgray));
mBottomBar.setActiveTabColor(getResources().getColor(R.color.laborswipe_lightgray));
// Make a Badge for the second tab, with red background color and a value of "13".
BottomBarBadge unreadMessages = mBottomBar.makeBadgeForTabAt(1, getResources().getColor(R.color.laborswipe_orange), 5);
// Control the badge's visibility
unreadMessages.show();
//unreadMessages.hide();
// Change the displayed count for this badge.
//unreadMessages.setCount(4);
// Change the show / hide animation duration.
unreadMessages.setAnimationDuration(200);
// If you want the badge be shown always after unselecting the tab that contains it.
unreadMessages.setAutoShowAfterUnSelection(true);
// If you don't want this badge to be hidden after selecting the tab contains it.
unreadMessages.setAutoShowAfterUnSelection(false);
}
In my adapter, I am trying to open the fragment when you click a button, like this:
holder.desc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Open Description", Toast.LENGTH_SHORT).show();
JobDescFragment firstFragment = new JobDescFragment();
((MainActivity)context).getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
});
If I comment out the call to setupBottomToolbar() in my onCreate of the activity, the fragment opens up fine...but that means I don't have the bottom bar...
What am I missing? There has to be a way to use the bottom bar and also open a fragment?
Thanks!
EDIT:
Here is the top part of my activity.
public class MainActivity extends AppCompatActivity {
private ArrayList<String> swipecardsList;
private ArrayList<Job> jobList = new ArrayList<Job>();
private JobAdapter arrayAdapter; //arrayadapter
private BottomBar mBottomBar;
SharedPreferences settings;
#InjectView(R.id.frame) SwipeFlingAdapterView flingContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//color the notification bar with our company colors
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.laborswipe_notificationbar));
//remove title from action bar and add the logo to the top left of the action bar
setupTopToolbar();
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
//set up the bottom toolbar using the roughike library to mimic android material design
setupBottomToolbar(savedInstanceState);
My adapter:
public class JobAdapter extends ArrayAdapter<Job> {
private final Context context;
private final ArrayList<Job> jobs;
private final int layoutResourceId;
private final SwipeFlingAdapterView flingContainer;
private boolean isExpanded = false;
public JobAdapter(Context context, int layoutResourceId, ArrayList<Job> jobs, SwipeFlingAdapterView flingContainer) {
super(context, layoutResourceId, jobs);
this.context = context;
this.jobs = jobs;
this.layoutResourceId = layoutResourceId;
this.flingContainer = flingContainer;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
String pay, hrs;
final Bundle fragmentParams = new Bundle();
LayoutInflater inflater = LayoutInflater.from(context);
if (view == null) {
view = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.title = (TextView)view.findViewById(R.id.tv_jobTitle);
holder.desc = (TextView) view.findViewById(R.id.tv_JobDesc);
view.setTag(holder);
} else {
holder = (ViewHolder)view.getTag();
}
Job j = jobs.get(position);
holder.title.setText(j.getJobTitle());
holder.desc.setText(j.getDescription());
//when user clicks apply, swipe the card right
holder.apply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Open up a fragment to display the entire job description
Toast.makeText(context, "Applied", Toast.LENGTH_SHORT).show();
flingContainer.getTopCardListener().selectRight();
}
});
//when user clicks dismiss, swipe the card left
holder.dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Open up a fragment to display the entire job description
Toast.makeText(context, "Dismissed", Toast.LENGTH_SHORT).show();
flingContainer.getTopCardListener().selectLeft();
}
});
//on click event listener for the job description field - open larger window to read description
holder.desc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(context, "Open Description", Toast.LENGTH_SHORT).show();
JobDescFragment firstFragment = new JobDescFragment();
Fragment frag = new Fragment();
frag = firstFragment.newJobDescFrag(j.getDescription());
((MainActivity) context).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, frag)
.addToBackStack("JobDesc").commit();
}
});
return view;
}
public class ViewHolder
{
TextView title;
TextView payrate;
TextView dateRange;
TextView workinghrs;
TextView location;
TextView companyname;
TextView desc;
TextView experience;
TextView equipment;
Button apply, dismiss, expand;
}
}
activity_main.xml:
<merge
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">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
android:id="#+id/frame"
android:background="#color/laborswipe_lightgray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:layout_gravity="top" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</merge>
Fragment Layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".JobDescFragment">
<LinearLayout
android:id="#+id/outerDescriptionLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical"
android:background="#drawable/swipecard_shadow"
android:gravity="top"
android:layout_marginLeft="5dp">
<LinearLayout
android:id="#+id/DescriptionLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical"
android:weightSum="1"
android:gravity="top"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#00FF00"
android:clickable="true">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="Detailed Description:"
android:textColor="#000000"
android:id="#+id/tv_title" />
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="THIS IS THE FULL DESCRIPTION"
android:textColor="#000000"
android:id="#+id/tv_fullDescription" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
Logcat:
08-07 11:20:47.799 13896-13896/com.lorentzos.swipecards.example I/System.out: DEBUG: job desc fragment loaded!
08-07 11:20:47.855 13896-13941/com.lorentzos.swipecards.example W/EGL_emulation: eglSurfaceAttrib not implemented
08-07 11:20:47.855 13896-13941/com.lorentzos.swipecards.example W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaa7f880, error=EGL_SUCCESS
08-07 11:20:48.002 13896-13941/com.lorentzos.swipecards.example V/RenderScript: 0xa1408000 Launching thread(s), CPUs 2
08-07 11:20:49.798 13896-13941/com.lorentzos.swipecards.example E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae433ca0
When I use the bottom bar (not working- no fragment opened but toast displayed):
When I don't use the bottom bar (workin-fragment opened, background is green):
try to link a pic of problem and without problem(no bottombar) and since you are using merge the layout hierarchy will be laid off according to your activity's viewgroup(linear,relative) constraints(we don't know what they are like).
as you said when there is no bottombar ,you fragment displays perfectly though when the bottombar it there ,problem stats ,as per your log in fragment indicating that your fragment is loading perfectly even though when bottombar is visible mean fragment is there but is not visible ,seems like your fragment didn't get the appropriate space to get displayed.
other solution can be adding bottom bar to your fragment instead of activity to avoid any overlapping ,like
mBottomBar.attach(findViewById(R.id.fragmentContainer), savedInstanceState);
OK, I think the solution for this should be simple, from what I can see in your code, you are attaching the BottomBar to your activity, I think this is the problem. If you were to read the readme.md in the roughike/BottomBar github page you'd find this
Why is it overlapping my Navigation Drawer?
All you need to do is instead of attaching the BottomBar to your Activity, attach it to the view that has your content. For example, if your fragments are in a ViewGroup that has the id fragmentContainer, you would do something like this:
mBottomBar.attach(findViewById(R.id.fragmentContainer), savedInstanceState);
So, since navigation drawer works with transition a fragment in and out of view with animation, the same thing is happening when you are adding a new fragment to your activity.
The Solution
From what I can see in your code, your fragment container id is this: fragment_container in your activity layout. So according to the documentation you'd just need to do attach your bottomBar to the fragment_container instead of MainActivity.this
mBottomBar.attach(findViewById(R.id.fragment_container), savedInstanceState);
If the above doesn't work try this
What you'd need to do is add an extra FrameLayout to hold your bottombar, which has a transparent background, but is on top of your fragment.
So change your main_activity layout to
<merge
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">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
android:id="#+id/frame"
android:background="#color/laborswipe_lightgray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:layout_gravity="top" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/holder_bottombar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
</merge>
Now in the code instead of attaching the bottom bar to mainactivity, just attach it to the holder like so
mBottomBar.attach(findViewById(R.id.holder_bottombar), savedInstanceState);

ImageView inside ListView is ignoring all layout dimensions and filling the whole screen

I'm trying to create a set of 4 dynamically updating listviews of icons.
To do this, I've put my 4 listviews inside a gridview, and I'm using a custom layout for each listview item with a single imageview inside it. I'm handling changing the icon inside a custom adapter. I'm using the Picasso library to handle the icons, and all my icons are stored locally in drawable as .png files.
I'm not getting any errors, but when I run the app, all I get is a single icon that takes up the whole screen. I have a small imageview and a textview above the gridview that contains the lists of icons, but the one icon pushes even that out of the way.
This is my layout preview, and a screenshot of the app running in an emulator. (the result is the same if I run on my phone).
preview,
running
(apologies for not embedding, I'm new to SO, and don't have 10 rep yet haha)
I've tried resizing the image at runtime inside my custom adapter. I've tried setting the maximum width of the listview - both at runtime (since I want to adapt to different screen dimensions), and in the xml files. I've tried resizing the source .png files outside of android studio. Nothing's worked.
My activity xml file:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.sta.tomov0.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tomoFace"
android:src="#drawable/office38"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_below="#+id/tomoFace"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="false"
android:layout_marginBottom="50dp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:rowCount="1"
android:id="#+id/gridLayout"
android:layout_below="#+id/textView"
android:columnCount="5">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewA"
android:layout_row="0"
android:layout_column="0" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewB"
android:layout_row="0"
android:layout_column="1" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewC"
android:layout_row="0"
android:layout_column="2" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewD"
android:layout_column="3"
android:layout_row="0" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="4"
android:layout_row="0"
android:onClick="addTask"
android:src="#android:drawable/stat_notify_more" />
</GridLayout>
</RelativeLayout>
My listview layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listImageView"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
and my activity.java file:
public class MainActivity extends AppCompatActivity {
ImageView tomoface;
ListView listViewA;
ArrayList arrayListA = new ArrayList<Integer>();
Boolean aExists = false;
ListView listViewB;
ArrayList arrayListB = new ArrayList<Integer>();
Boolean bExists = false;
ListView listViewC;
ArrayList arrayListC = new ArrayList<Integer>();
Boolean cExists = false;
ListView listViewD;
ArrayList arrayListD = new ArrayList<Integer>();
Boolean dExists = false;
Button addButton;
TextView tomoText;
File tomoFiles;
File taskFiles;
String currentCommunity = "Dep";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Paper.init(getApplicationContext());
tomoface = (ImageView) findViewById(R.id.tomoFace);
tomoface.setImageResource(R.drawable.favourite15);
tomoText = (TextView) findViewById(R.id.textView);
listViewA = (ListView) findViewById(R.id.listViewA);
listViewB = (ListView) findViewById(R.id.listViewB);
listViewC = (ListView) findViewById(R.id.listViewC);
listViewD = (ListView) findViewById(R.id.listViewD);
tomoFiles = getDir("TomoFiles", MODE_PRIVATE);
taskFiles = new File(tomoFiles, "taskFiles");
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width =(size.x)/5;
ViewGroup.LayoutParams params = listViewA.getLayoutParams();
params.height = width;
params.width = width;
listViewA.setLayoutParams(params);
listViewB.setLayoutParams(params);
listViewC.setLayoutParams(params);
listViewD.setLayoutParams(params);
/*
If there is no data saved, the following code creates a list of (empty) lists to hold TaskClass objects
The 4 sub lists each represent one category.
If there is data saved, the following code retrieves that data, and creates 4 new ArrayLists,
each containing the iconIds of the TaskClass objects in the corresponding position of the corresponding TaskClass arraylist.
These ArrayLists of ids are then used to populate the 4 listviews.
*/
ArrayList<ArrayList<TaskClass>> listList = Paper.book(currentCommunity).read("listList", new ArrayList<ArrayList<TaskClass>>());
if (listList.size() == 0){
listList.add(new ArrayList<TaskClass>());
listList.add(new ArrayList<TaskClass>());
listList.add(new ArrayList<TaskClass>());
listList.add(new ArrayList<TaskClass>());
} else {
for(TaskClass t:listList.get(0)){
arrayListA.add(t.getIcon());
}
for(TaskClass t:listList.get(1)){
arrayListB.add(t.getIcon());
}
for(TaskClass t:listList.get(2)){
arrayListC.add(t.getIcon());
}
for(TaskClass t:listList.get(3)){
arrayListD.add(t.getIcon());
}
}
Integer[] intArrayA = new Integer[arrayListA.size()];
for (int i = 0; i < arrayListA.size(); i++){
intArrayA[i] =(Integer) arrayListA.get(i);
}
ArrayAdapter adapterA = new CustomArrayAdapter(getApplicationContext(),intArrayA);
listViewA.setAdapter(adapterA);
Integer[] intArrayB = new Integer[arrayListB.size()];
for (int i = 0; i < arrayListB.size(); i++){
intArrayB[i] =(Integer) arrayListB.get(i);
}
ArrayAdapter adapterB = new CustomArrayAdapter(getApplicationContext(),intArrayB);
listViewB.setAdapter(adapterB);
Integer[] intArrayC = new Integer[arrayListC.size()];
for (int i = 0; i < arrayListC.size(); i++){
intArrayC[i] =(Integer) arrayListC.get(i);
}
ArrayAdapter adapterC = new CustomArrayAdapter(getApplicationContext(),intArrayC);
listViewC.setAdapter(adapterC);
Integer[] intArrayD = new Integer[arrayListD.size()];
for (int i = 0; i < arrayListD.size(); i++){
intArrayD[i] =(Integer) arrayListD.get(i);
}
ArrayAdapter adapterD = new CustomArrayAdapter(getApplicationContext(),intArrayD);
listViewD.setAdapter(adapterD);
}
//TODO: create a custom adapter that will display icons correctly
public class CustomArrayAdapter extends ArrayAdapter{
private final Context context;
private final Integer[] values;
public CustomArrayAdapter(Context context, Integer[] values) {
super(context, -1, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View iconView = inflater.inflate(R.layout.iconlayout, parent, false);
ImageView imageView = (ImageView) iconView.findViewById(R.id.listImageView);
int s =(int) values[position];
imageView.setImageResource(s);
//get the device width, to calculate icon width, and then set icon width accordingly
//TODO: setting icon width not working
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width =(size.x)/5;
Uri uri = Uri.parse("android.resource://com.example.sta.tomov0/drawable/"+s);
Picasso.with(context).load(uri).resize(width,width).fit().centerCrop().into(imageView);
return iconView;
}
}
}
A note: I have a custom data management class that returns arraylists. That's tested separately and working fine. The arraylists that are being fed to the adapter are int arraylists of the drawable references.
Help! What am I doing wrong? I've been searching through SO all day and trying different things. The solutions posted in these questions haven't helped T-T:
How to set ImageView width in android ListView?
ImageView in ListView that expands maximum width
(that was a bit difficult to implement, and just created other problems - it seems like overkill to create a custom view class just for displaying an icon?)
Give fixed height to your Imageview in My listview layout:
Say Something like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="120dp"
android:id="#+id/listImageView"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
I'm assuming #drawable/office38 is the image shown in your screenshot (taking up the whole screen)?
Try setting the dimensions of this image to something small, eg:
android:layout_width="50dp"
android:layout_height="50dp"
I know this may not be what you are actually looking to do, but at least it will show you if the problem is with that first ImageView or not and will free up space below it for the TextView and the GridLayout. It could be that the drawable is massive, and as you aren't specifying a set width/height or any scale options, it takes up all the space it can.
Also, have a look at http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType which describes the different scaling options you have with an ImageView.
GOT IT.
sorry guys. It was a really dumb problem after all.
My listview adapter is working fine it turns out. The problem was in the imageview that was outside of the lists (tomoface) - for some reason, I decided to set it to a different drawable on runtime, and hadn't scaled that at all haha.

How to Change background of popup layout?

I have created a popup and I have set one background of popup.
My xml file is as below. I want to change background of popup window.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pp1" >
<WebView
android:id="#+id/webview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginTop="30dp" />
My showPopup method is as below. So how can I change my popup background? and also I load a webview in popup window.
private void showPopup(final Activity context, Point p) {
int popupWidth = 720;
int popupHeight = 380;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
if (i == 1) {
WebView wv = (WebView) layout.findViewById(R.id.webview1);
wv.loadUrl("file:///android_asset/aboutus.html");
}
/*
* Resources res = getResources(); Drawable drawable =
* res.getDrawable(R.drawable.newImage); LinearLayout linearLayout =
* (LinearLayout)findViewById(R.id.GameLayout);
* linearLayout.setBackgroundDrawable(drawable);
*/
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 130;
int OFFSET_Y = 100;
// Clear the default translucent background
// popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
+ OFFSET_Y);
// Getting a reference to Close button, and close the popup when
// clicked.
}
Try this,
Change background from xml file:
in layout tag in your xml file write
android:background="#drawable/image">
and add the image in your drawable folder, make sure the image is of .png format.
Change background at runtime:
btn.setBackground(this.getResources().getDrawable(R.drawable.yellow_button));
Or
Instead this, you can write your activity name with this
btn.setBackground(MainActivity.this.getResources().getDrawable(R.drawable.yellow_button));

Android Custom Action bar not filling parent, title not center aligned

I have been experimenting the following issue: I have created a custom action bar:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/com_facebook_blue" >
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_centerInParent="true"
android:textSize="30dp"
android:textColor="#ffffff"
android:text="#string/app_title"/>
</RelativeLayout>
Which is inflated by a GraphicsBuilder class in the following way:
public static void setupActionBar(String title, Activity a) {
final ViewGroup actionBarLayout = (ViewGroup) a.getLayoutInflater()
.inflate(R.layout.action_bar_layout, null);
final ActionBar actionBar = a.getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
final int actionBarColor = a.getResources().getColor(R.color.main_red);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
TextView tv = (TextView) actionBarLayout.findViewById(R.id.action_bar_title);
Typeface font = Typeface.createFromAsset(a.getAssets(), "fonts/Molle-Regular.ttf");
tv.setText(title);
tv.setTypeface(font);
}
Which leads to the following results (I have kept the title bar background blue to highlight the issue):
Basically it looks like that the custom action bar does not fill the parent width, therefore any try to align the title at the center is useless. How can I fix this?
I know this is a long time gone.
Solved it by changing the layout params once it's been set as custom view.
My logic was that when you inflate it the first time, it assumes the size of the original container.
When you call:
actionBar.setCustomView(titlebar);
It's already got its dimensions preset.
My solution was:
View titlebar = inflater.inflate(R.layout.titlebar, null);
// Do stuff to Title Bar //
ActionBar actionBar = getActionBar();
actionBar.setCustomView(titlebar);
View v = actionBar.getCustomView();
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
v.setLayoutParams(lp);
Alternatively, you can first set the action bar layout without the inflater:
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.id.titlebar);
View v = actionBar.getCustomView();
// Do something to the view E.g Set button listeners blah //
// Rest of code

Categories