How do I get the line in sync with the round ball images and how to change the text and image when clicked ?
I'd like to do something like this:
https://www.behance.net/gallery/13564677/Blog
I got the ListView but I just need help in building the straight line pass through the round ball images.
Thanks for your help!
My Code Follows:
My activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d3d3d3"
>
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<ListView
android:id="#+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:divider="#null"
android:layout_gravity="left"
android:background="#A55676"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
custom_row.xml
?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="horizontal"
android:paddingLeft="35dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:orientation="horizontal"
android:background="#drawable/border"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="-5sp"
android:paddingTop="30dp"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="25dp"
android:drawablePadding="20dp"
android:paddingLeft="3dp"
android:paddingTop="25dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
MainActivity.java
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnItemClickListener {
private DrawerLayout drawerLayout;
private ListView listview;
private ActionBarDrawerToggle drawerListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView) findViewById(R.id.drawerList);
drawerLayout=(DrawerLayout)findViewById(R.id.drawerlayout);
MyAdapter myAdapter = new MyAdapter(this);
listview.setAdapter(myAdapter);
drawerLayout.setDrawerShadow(R.drawable.navbar_shadow, Gravity.LEFT);
drawerListener = new ActionBarDrawerToggle(this, drawerLayout,R.drawable.ic_drawer,R.string.drawer_open, R.string.drawer_close)
{
#Override
public void onDrawerClosed(View drawerView)
{
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Drawer Closed", Toast.LENGTH_LONG).show();
}
#Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Drawer Opened", Toast.LENGTH_LONG).show();
}
};
drawerLayout.setDrawerListener(drawerListener);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setIcon(R.color.transparent);
listview.setOnItemClickListener(this);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}
public void selectItem(int position)
{// TODO Auto-generated method stub
listview.setItemChecked(position, true);
//setTitle(planets[position]);
}
public void setTitle(String title)
{
getActionBar().setTitle(title);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(drawerListener.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{ // TODO Auto-generated method stub
ImageView titleImageView = (ImageView)view.findViewById(R.id.imageView1);
TextView titleTextView = (TextView)view.findViewById(R.id.textView1);
switch(position)
{
case 0:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState()){
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
case 1:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
case 2:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
case 3:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
Intent intent = new Intent(getApplicationContext(),Featured.class);
startActivity(intent);
break;
case 4:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
case 5:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
default:
break;
}
}
}
class MyAdapter extends BaseAdapter
{
String[] socialSites;
int [] images = {R.drawable.ic_new,R.drawable.ic_new,R.drawable.ic_new,R.drawable.ic_new,R.drawable.ic_new,R.drawable.ic_new};
Context context;
public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
socialSites = context.getResources().getStringArray(R.array.social);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return socialSites.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return socialSites[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_row,parent, false);
}
else
{
row=convertView;
}
TextView titleTextView = (TextView) row.findViewById(R.id.textView1);
ImageView titleImageView = (ImageView) row.findViewById(R.id.imageView1);
titleTextView.setText(socialSites[position]);
titleImageView.setImageResource(images[position]);
return row;
}
}
Use a ListView, and assign a 9-patch drawable to each item's background which will consist of a centered dot and the vertical line.
The whole trick will be to specify the strechable regions of your nine-patch properly (stretch the line and not the dot).
Once you are happy with that, just make a different dot appearance for the active element and use an XML selector drawable to assign either the default or activated state 9 patch to the background.
Related
I have gone through a lot of links here, but yet i was not able to get my stuff working. Can you please help me fetch data from my custom list.
Details are as under :- My custom list view
<?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" >
<RadioGroup
android:id="#+id/radioGroup_option"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tv_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"/>
<RadioButton
android:id="#+id/rb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/rb_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
</LinearLayout>
I am populating a List through the UI the code for that is :-
package com.example.customadapter;
import java.lang.reflect.Array;
import java.util.Arrays;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class CustomAdapter extends ActionBarActivity {
AddDataToArray mydata;
EditText qstno,qstn,opt1,opt2;
Button btn_save,btn_display;
int questio_no;
String question,option1,option2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_adapter);
mydata=AddDataToArray.getInstance();
qstno=(EditText)findViewById(R.id.edt_qstn_no);
qstn=(EditText)findViewById(R.id.edt_add_qstn);
opt1=(EditText)findViewById(R.id.edt_opt1);
opt2=(EditText)findViewById(R.id.edt_opt2);
btn_save=(Button)findViewById(R.id.btn_save);
btn_display=(Button)findViewById(R.id.btn_display);
btn_save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
questio_no=Integer.parseInt(qstno.getText().toString());
question=qstn.getText().toString();
option1=opt1.getText().toString();
option2=opt2.getText().toString();
System.out.println("Questio added in the UI --> "+ question);
String statusReceived=mydata.AddingQuestions(questio_no, question, option1, option2);
Toast.makeText(getApplicationContext(), statusReceived, Toast.LENGTH_LONG).show();
qstn.setText("");
opt1.setText("");
opt2.setText("");
}
});
btn_display.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i=0;i<mydata.myQstnList.size();i++)
{
System.out.println("Qustion no"+mydata.myQstnList.get(i).getQstno());
System.out.println("Question -->"+mydata.myQstnList.get(i).getQstn());
//System.out.println("The First Option added was:-" + mydata.myQstnList.get(i).getOpt1());
}
Intent myIntent = new Intent(CustomAdapter.this, Mediator.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.custom_adapter, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Finally i made an adapter from base adapter as follows:-
package com.example.customadapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Question> mQuestion;
Context context;
public MyAdapter(Context context,List<Question> mQuestion) {
super();
this.context=context;
this.mQuestion = mQuestion;
this.mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mQuestion.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mQuestion.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view;
ViewHolder holder;
if (convertView == null){
view = mInflater.inflate(R.layout.customized_view, parent,false);
holder = new ViewHolder();
holder.rg=(RadioGroup)view.findViewById(R.id.radioGroup_option);
holder.tv_qstn=(TextView)view.findViewById(R.id.tv_id);
holder.rb_opt1=(RadioButton)view.findViewById(R.id.rb_1);
holder.rb_opt2=(RadioButton)view.findViewById(R.id.rb_2);
view.setTag(mQuestion);
}else {
view = convertView;
holder = (ViewHolder)view.getTag();
}
Question qstnRef=mQuestion.get(position);
holder.tv_qstn.setText(qstnRef.getQstn());
holder.rb_opt1.setText(qstnRef.getOpt1());
holder.rb_opt2.setText(qstnRef.getOpt2());
holder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//List<Question> data =(List<Question>) group.getTag();
//Toast.makeText(context,(CharSequence) data.get(checkedId).getOpt2(),Toast.LENGTH_SHORT).show();
int childCount=group.getChildCount();
for (int i=0;i<childCount;i++){
RadioButton r_btn=(RadioButton)group.getChildAt(i);
if (r_btn.getId() == checkedId){
System.out.println(r_btn.getText().toString());
}
}
}
});
return view;
}
private class ViewHolder {
public TextView tv_qstn;
public RadioButton rb_opt1,rb_opt2;
public RadioGroup rg;
}
}
But i am trying to get this data that i enter in the UI, but I am not able to get this to work, please provide me assistance !!!!
The code in the current format is giving me runtime error :-
05-09 06:01:06.669: E/AndroidRuntime(2420):
java.lang.ClassCastException: android.widget.TextView cannot be cast
to android.widget.RadioButton
05-09 06:01:06.669: E/AndroidRuntime(2420): at
com.example.customadapter.MyAdapter$1.onCheckedChanged(MyAdapter.java:83)
group.getChildCount() returns 3 as it has 3 child. but 1 child is of TextView that's why you were getting java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioButton
To avoid it do type checking using instanceof before typecasting.
do like this:
int childCount=group.getChildCount();
for (int i=0;i<childCount;i++){
if(group.getChildAt(i) instanceof RadioButton ) { // check if child is `RadioButton`
RadioButton r_btn = (RadioButton) group.getChildAt(i);
if (r_btn.getId() == checkedId) {
System.out.println(r_btn.getText().toString());
}
}
}
My homework is a Text to speech app using Spinner choose Language.
but I do not know very much about this topic.
Here my code for MainActivity.java:
package com.detainhom6.doctext;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity implements OnInitListener{
// Khai bao cac bien can thiet
String chuoinhap;
String ngonngu[] = {"English", "Japan", "Korea", "China"};
EditText txtNhap;
Button btnNoi;
Toast note;
Spinner spnn;
//Khai bao bien text to speech
private TextToSpeech t2p;
private int kiemtradulieu = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// gan gia tri cho bien vua khai bao
txtNhap = (EditText)findViewById(R.id.txtnhap);
btnNoi = (Button)findViewById(R.id.btnNoi);
spnn = (Spinner)findViewById(R.id.menungonngu);
//
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ngonngu);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spnn.setAdapter(adapter);
spnn.setOnItemSelectedListener(new ClickChonNgonNgu());
Intent kiemtrat2pintent = new Intent();
kiemtrat2pintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(kiemtrat2pintent, kiemtradulieu);
btnNoi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View mangV) {
// TODO Auto-generated method stub
chuoinhap = txtNhap.getText().toString();
doc(chuoinhap);
}
});
}
// Tao Ham khi Goi Intent Acivity Result
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == kiemtradulieu){
if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
t2p = new TextToSpeech(this,this);
}
else
{
Intent caidatt2pIntent = new Intent();
caidatt2pIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(caidatt2pIntent);
}
}
}
// Khoi Tao Ham Doc Chuoi Da Nhap
protected void doc(String chuoinhap2) {
// TODO Auto-generated method stub
t2p.speak(chuoinhap2, TextToSpeech.QUEUE_FLUSH, null);
}
// Tao su kien khi chon item trong Spinner
public class ClickChonNgonNgu implements OnItemSelectedListener{
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String nndachon;
nndachon = ngonngu[position];
if(nndachon == "English"){
t2p.setLanguage(Locale.US);
note = Toast.makeText(MainActivity.this, "Bạn Dã Chọn English", Toast.LENGTH_SHORT);
note.setGravity(Gravity.CENTER, 0, 0);
note.show();
}
if(nndachon == "Japan"){
t2p.setLanguage(Locale.JAPAN);
note = Toast.makeText(MainActivity.this, "Bạn đã chọn Japan", Toast.LENGTH_SHORT);
note.setGravity(Gravity.CENTER, 0, 0);
note.show();
}
if(nndachon == "Korea"){
t2p.setLanguage(Locale.KOREA);
note = Toast.makeText(MainActivity.this, "Bạn đã chọn Korea", Toast.LENGTH_SHORT);
note.setGravity(Gravity.CENTER, 0, 0);
note.show();
}
if(nndachon == "China"){
t2p.setLanguage(Locale.CHINA);
note = Toast.makeText(MainActivity.this, "Bạn đã chọn China", Toast.LENGTH_SHORT);
note.setGravity(Gravity.CENTER, 0, 0);
note.show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status == TextToSpeech.SUCCESS)
{
t2p.setLanguage(Locale.US);
Log.e("TTS INIT", "TTS Thành Công");
}
else if(status == TextToSpeech.ERROR)
{
Log.e("TTS INIT", "TTS Lỗi");
}
}
}
And here is the activity_main.xml:
<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.detainhom6.doctext.MainActivity" >
<TextView
android:id="#+id/txtnote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/note1" />
<Button
android:id="#+id/btnNoi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtnhap"
android:layout_below="#+id/txtnhap"
android:layout_marginTop="21dp"
android:text="#string/Button_Noi" />
<EditText
android:id="#+id/txtnhap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtnote"
android:layout_below="#+id/txtnote"
android:layout_marginTop="116dp"
android:ems="10"
android:hint="#string/hint_txt" >
<requestFocus />
</EditText>
<Spinner
android:id="#+id/menungonngu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtnhap"
android:layout_below="#+id/txtnote"
android:layout_marginTop="34dp" />
</RelativeLayout>
Can someone please help me fix code if you can?
Thanks so much!
I get an error whenever I run the app with the setOnClickListener method. Otherwise it works fine. Can you guys please help me?
package com.grozav.meetmeup;
import com.grozav.meetmeup.R;
import com.grozav.meetmeup.library.UserFunctions;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
public class DashboardActivity extends FragmentActivity implements TabListener {
UserFunctions userFunctions;
ViewPager viewPager;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
userFunctions = new UserFunctions();
if (!userFunctions.isUserLoggedIn(getApplicationContext())) {
// user is not logged in show login screen
Intent login = new Intent(getApplicationContext(),
LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int arg0) {
actionBar.setSelectedNavigationItem(arg0);
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabA = actionBar.newTab();
tabA.setText("My Profile");
tabA.setTabListener(this);
ActionBar.Tab tabB = actionBar.newTab();
tabB.setText("Meet Me Up");
tabB.setTabListener(this);
actionBar.addTab(tabA);
actionBar.addTab(tabB);
/*********************************
I GET THE ERROR HERE
*********************************/
Button logout = (Button) findViewById(R.id.btnLogout);
logout.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(getApplicationContext(), LoginActivity.class); // fix View.getContext() to getContext()
startActivity(myIntent); // change to startActivity
}
});
}
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
viewPager.setCurrentItem(arg0.getPosition());
}
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
Fragment fragment = null;
if (arg0 == 0) {
fragment = new FragmentA();
}
if (arg0 == 1) {
fragment = new FragmentB();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
dashboard.xml file
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashboardActivity" />
fragment_a.xml file
<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"
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=".FragmentA" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnLogout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Logout" />
</LinearLayout>
</FrameLayout>
This is my first android project so please don't be too harsh. Your help is greatly appreciated! Thank you very much!
You need to write your UI events within according fragment, try this,
public class FragmentA extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_a.xml, null);
Button logout = (Button)view.findViewById(R.id.btnLogout);
logout.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(getActivity(), LoginActivity.class);
startActivity(myIntent); // change to startActivity
}
});
return view;
}
}
try this..
logout.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getApplicationContext(), LoginActivity.class); // fix View.getContext() to getContext()
startActivity(myIntent); /
}});
If you still face problem then change the button id in XML and try again.
have a look at the code below...
i am setting the onclick listener on the textview and the ontouch listener but none of them is working...
what seems to be the problem>....?
AlertDialog.Builder builder;
AlertDialog a;
TextView text,txtNewUser ;
ImageView image;
View layout;
Button ok;
String pswrd;
LayoutInflater inflater;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final Context mContext = this;
Dialog dialog = new Dialog(mContext);
inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
ok = (Button) findViewById(R.id.buttonOK);
dialog.setContentView(R.layout.custom_dialog_private_space);
dialog.setTitle("Password");
dialog.setCancelable(true);
layout = inflater.inflate(R.layout.custom_dialog_private_space,
(ViewGroup) findViewById(R.id.layout));
text = (TextView) dialog.findViewById(R.id.tvDesc);
txtNewUser = (TextView) dialog.findViewById(R.id.tvNewUser);
image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.lock_symbol_android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
a = builder.create();
a.show();
txtNewUser.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
return true;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}
here is the code of textview from the xml of the dialog layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/layout" >
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tvNewUser"
android:text="New User?"
android:layout_gravity="top|left"
android:clickable="true"
/>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/tvDesc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Enter password"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="100"
android:inputType="textPassword" >
</EditText>
<Button
android:id="#+id/buttonOK"
android:layout_width="100dp"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:text="OK" />
</LinearLayout>
Instead of infalter use window class
return false in on Touch listener
set contenet view to your main activity
package com.collabera.labs.sai;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class HomePage extends Activity {
AlertDialog.Builder builder;
AlertDialog a;
TextView text, txtNewUser;
ImageView image;
Button ok;
String pswrd;
public Window mWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_private_space);
ok = (Button) findViewById(R.id.buttonOK);
final Context mContext = this;
Dialog dialog = new Dialog(HomePage.this);
dialog.setContentView(R.layout.custom_dialog_private_space);
dialog.setTitle("Password");
dialog.setCancelable(true);
dialog.show();
mWindow = dialog.getWindow();
Log.w(" dialog is " + mWindow.toString(), "-------");
text = (TextView) mWindow.findViewById(R.id.tvDesc);
txtNewUser = (TextView) mWindow.findViewById(R.id.tvNewUser);
image = (ImageView) mWindow.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
txtNewUser.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Touched", Toast.LENGTH_SHORT)
.show();
Log.v("AS", "Touched");
return false;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}
}
I think.. you should also return
return super.onTouchEvent(event);
from onTouch function.. instead of true because true means the event is consumed by touch and other actions are not called for this event.. in this case onClick
this is the way i used to make imageview clickable:
public class Main extends Activity **implements OnClickListener** {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView profile_btn=(ImageView) findViewById(R.id.x_Btn);
x_btn.setOnClickListener(this) ;
ImageView food_btn=(ImageView) findViewById(R.id.y_Btn);
ybtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.x_Btn:
//do some thing;break;
case R.id.y_Btn:
//do some thing;break;
}
}
}
In toast both cases you have written clicked so change to touch in onTouch listener and return false at onTouch Listener:
public class HomePage extends Activity {
private TextView txtNewUser;
private Context mContext;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = getApplicationContext();
txtNewUser = (TextView) findViewById(R.id.tvNewUser);
txtNewUser.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Touched ", Toast.LENGTH_SHORT)
.show();
Log.v("AS", "Touched");
return false;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}
I need to put button into list view to go to another view.
my button: AddTime = (Button) findViewById(R.id.Add_Time);
i tryd to get it working using:
public class myclass extends ListActivity implements OnClickListener {
and i have
public void onClick(View arg0) {
}
and i need it to go to another place i think using this
Intent intent = new Intent(this, myclass.class);
startActivity(intent);
how to i get this all working in listview?
use this
package example.buttonWithList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class buttonWithList extends ListActivity {
/** Called when the activity is first created. */
String[] items={"azhar","j"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new bsAdapter(this));
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
// TODO Auto-generated constructor stub
this.cntx=context;
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return items.length;
}
#Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return items[position];
}
#Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return items.length;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
// TODO Auto-generated method stub
/*if(convertView==null)
{
}*/
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.row, null);
TextView tv=(TextView)row.findViewById(R.id.txtRow);
Button btn=(Button)row.findViewById(R.id.btnRow);
tv.setText(items[position]);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onListItemClick(this, this, position, null);
}
});
return row;
}
protected void onListItemClick(OnClickListener onClickListener,
OnClickListener onClickListener2, int position, Object object) {
// TODO Auto-generated method stub
//Toast.makeText(this, items[position], 3000);
System.out.println(items[position]);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<ListView android:id="#android:id/list" android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:text="#+id/TextView01" android:id="#+id/txtRow"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:id="#+id/btnRow" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Click "></Button>
</LinearLayout>
I suggest you to use a list view with strings. Make a buttons array and when you click on one, via onClick get the button in the array with the position parameter